user32: Dont create 16 bits heap in 64-bits mode
[wine/wine64.git] / dlls / ddraw / ddraw.c
blob8033b153dec1b7f568d7f7f9d5089d9081047555
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 /*****************************************************************************
66 * IUnknown Methods
67 *****************************************************************************/
69 /*****************************************************************************
70 * IDirectDraw7::QueryInterface
72 * Queries different interfaces of the DirectDraw object. It can return
73 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
74 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
75 * method.
76 * The returned interface is AddRef()-ed before it's returned
78 * Used for version 1, 2, 4 and 7
80 * Params:
81 * refiid: Interface ID asked for
82 * obj: Used to return the interface pointer
84 * Returns:
85 * S_OK if an interface was found
86 * E_NOINTERFACE if the requested interface wasn't found
88 *****************************************************************************/
89 static HRESULT WINAPI
90 IDirectDrawImpl_QueryInterface(IDirectDraw7 *iface,
91 REFIID refiid,
92 void **obj)
94 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
96 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
98 /* Can change surface impl type */
99 EnterCriticalSection(&ddraw_cs);
101 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
102 *obj = NULL;
104 if(!refiid)
106 LeaveCriticalSection(&ddraw_cs);
107 return DDERR_INVALIDPARAMS;
110 /* Check DirectDraw Interfaces */
111 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
112 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
114 *obj = ICOM_INTERFACE(This, IDirectDraw7);
115 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
117 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
119 *obj = ICOM_INTERFACE(This, IDirectDraw4);
120 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
122 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
124 *obj = ICOM_INTERFACE(This, IDirectDraw3);
125 TRACE("(%p) Returning IDirectDraw3 interface at %p\n", This, *obj);
127 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
129 *obj = ICOM_INTERFACE(This, IDirectDraw2);
130 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
132 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
134 *obj = ICOM_INTERFACE(This, IDirectDraw);
135 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
138 /* Direct3D
139 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
140 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
141 * who had this idea and why. The older interfaces can query and IDirect3D version
142 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
143 * and messy to implement with the common creation function, so it has been left out here.
145 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
146 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
147 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
148 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
150 /* Check the surface implementation */
151 if(This->ImplType == SURFACE_UNKNOWN)
153 /* Apps may create the IDirect3D Interface before the primary surface.
154 * set the surface implementation */
155 This->ImplType = SURFACE_OPENGL;
156 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
158 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
160 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
161 ERR(" (%p) You may want to contact wine-devel for help\n", This);
162 /* Should I assert(0) here??? */
164 else if(This->ImplType != SURFACE_OPENGL)
166 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
167 /* Do not abort here, only reject 3D Device creation */
170 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
172 This->d3dversion = 1;
173 *obj = ICOM_INTERFACE(This, IDirect3D);
174 TRACE(" returning Direct3D interface at %p.\n", *obj);
176 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
178 This->d3dversion = 2;
179 *obj = ICOM_INTERFACE(This, IDirect3D2);
180 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
182 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
184 This->d3dversion = 3;
185 *obj = ICOM_INTERFACE(This, IDirect3D3);
186 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
188 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
190 This->d3dversion = 7;
191 *obj = ICOM_INTERFACE(This, IDirect3D7);
192 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
196 /* Unknown interface */
197 else
199 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
200 LeaveCriticalSection(&ddraw_cs);
201 return E_NOINTERFACE;
204 IUnknown_AddRef( (IUnknown *) *obj );
205 LeaveCriticalSection(&ddraw_cs);
206 return S_OK;
209 /*****************************************************************************
210 * IDirectDraw7::AddRef
212 * Increases the interfaces refcount, basically
214 * DDraw refcounting is a bit tricky. The different DirectDraw interface
215 * versions have individual refcounts, but the IDirect3D interfaces do not.
216 * All interfaces are from one object, that means calling QueryInterface on an
217 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
218 * IDirectDrawImpl object.
220 * That means all AddRef and Release implementations of IDirectDrawX work
221 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
222 * except of IDirect3D7 which thunks to IDirectDraw7
224 * Returns: The new refcount
226 *****************************************************************************/
227 static ULONG WINAPI
228 IDirectDrawImpl_AddRef(IDirectDraw7 *iface)
230 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
231 ULONG ref = InterlockedIncrement(&This->ref7);
233 TRACE("(%p) : incrementing IDirectDraw7 refcount from %u.\n", This, ref -1);
235 if(ref == 1) InterlockedIncrement(&This->numIfaces);
237 return ref;
240 /*****************************************************************************
241 * IDirectDrawImpl_Destroy
243 * Destroys a ddraw object if all refcounts are 0. This is to share code
244 * between the IDirectDrawX::Release functions
246 * Params:
247 * This: DirectDraw object to destroy
249 *****************************************************************************/
250 void
251 IDirectDrawImpl_Destroy(IDirectDrawImpl *This)
253 /* Clear the cooplevel to restore window and display mode */
254 IDirectDraw7_SetCooperativeLevel(ICOM_INTERFACE(This, IDirectDraw7),
255 NULL,
256 DDSCL_NORMAL);
258 /* Destroy the device window if we created one */
259 if(This->devicewindow != 0)
261 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
262 DestroyWindow(This->devicewindow);
263 This->devicewindow = 0;
266 /* Unregister the window class */
267 UnregisterClassA(This->classname, 0);
269 EnterCriticalSection(&ddraw_cs);
270 list_remove(&This->ddraw_list_entry);
271 LeaveCriticalSection(&ddraw_cs);
273 /* Release the attached WineD3D stuff */
274 IWineD3DDevice_Release(This->wineD3DDevice);
275 IWineD3D_Release(This->wineD3D);
277 /* Now free the object */
278 HeapFree(GetProcessHeap(), 0, This);
281 /*****************************************************************************
282 * IDirectDraw7::Release
284 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
286 * Returns: The new refcount
287 *****************************************************************************/
288 static ULONG WINAPI
289 IDirectDrawImpl_Release(IDirectDraw7 *iface)
291 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
292 ULONG ref = InterlockedDecrement(&This->ref7);
294 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
296 if(ref == 0)
298 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
299 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
302 return ref;
305 /*****************************************************************************
306 * IDirectDraw methods
307 *****************************************************************************/
309 /*****************************************************************************
310 * IDirectDraw7::SetCooperativeLevel
312 * Sets the cooperative level for the DirectDraw object, and the window
313 * assigned to it. The cooperative level determines the general behavior
314 * of the DirectDraw application
316 * Warning: This is quite tricky, as it's not really documented which
317 * cooperative levels can be combined with each other. If a game fails
318 * after this function, try to check the cooperative levels passed on
319 * Windows, and if it returns something different.
321 * If you think that this function caused the failure because it writes a
322 * fixme, be sure to run again with a +ddraw trace.
324 * What is known about cooperative levels (See the ddraw modes test):
325 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
326 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
327 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
328 * DDSCL_FULLSCREEN can be activated
329 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
331 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
332 * DDSCL_SETFOCUSWINDOW (partially),
333 * DDSCL_MULTITHREADED (work in progress)
335 * Unhandled flags, which should be implemented
336 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
337 * expect any difference to a normal window for wine)
338 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
339 * rendering (Possible test case: Half-life)
341 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
343 * These don't seem very important for wine:
344 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
346 * Returns:
347 * DD_OK if the cooperative level was set successfully
348 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
349 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
350 * (Probably others too, have to investigate)
352 *****************************************************************************/
353 static HRESULT WINAPI
354 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
355 HWND hwnd,
356 DWORD cooplevel)
358 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
359 HWND window;
361 TRACE("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
362 DDRAW_dump_cooperativelevel(cooplevel);
364 EnterCriticalSection(&ddraw_cs);
366 /* Get the old window */
367 window = This->dest_window;
369 /* Tests suggest that we need one of them: */
370 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
371 DDSCL_NORMAL |
372 DDSCL_EXCLUSIVE )))
374 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
375 LeaveCriticalSection(&ddraw_cs);
376 return DDERR_INVALIDPARAMS;
379 /* Handle those levels first which set various hwnds */
380 if(cooplevel & DDSCL_SETFOCUSWINDOW)
382 /* This isn't compatible with a lot of flags */
383 if(cooplevel & ( DDSCL_MULTITHREADED |
384 DDSCL_FPUSETUP |
385 DDSCL_FPUPRESERVE |
386 DDSCL_ALLOWREBOOT |
387 DDSCL_ALLOWMODEX |
388 DDSCL_SETDEVICEWINDOW |
389 DDSCL_NORMAL |
390 DDSCL_EXCLUSIVE |
391 DDSCL_FULLSCREEN ) )
393 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
394 LeaveCriticalSection(&ddraw_cs);
395 return DDERR_INVALIDPARAMS;
397 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
399 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
400 LeaveCriticalSection(&ddraw_cs);
401 return DDERR_HWNDALREADYSET;
404 This->focuswindow = hwnd;
405 /* Won't use the hwnd param for anything else */
406 hwnd = NULL;
408 /* Use the focus window for drawing too */
409 This->dest_window = This->focuswindow;
411 /* Destroy the device window, if we have one */
412 if(This->devicewindow)
414 DestroyWindow(This->devicewindow);
415 This->devicewindow = NULL;
418 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
419 if(cooplevel & DDSCL_NORMAL)
421 /* Can't coexist with fullscreen or exclusive */
422 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
424 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
425 LeaveCriticalSection(&ddraw_cs);
426 return DDERR_INVALIDPARAMS;
429 /* Switching from fullscreen? */
430 if(This->cooperative_level & DDSCL_FULLSCREEN)
432 /* Restore the display mode */
433 IDirectDraw7_RestoreDisplayMode(iface);
435 This->cooperative_level &= ~DDSCL_FULLSCREEN;
436 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
437 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
440 /* Don't override focus windows or private device windows */
441 if( hwnd &&
442 !(This->focuswindow) &&
443 !(This->devicewindow) &&
444 (hwnd != window) )
446 This->dest_window = hwnd;
449 else if(cooplevel & DDSCL_FULLSCREEN)
451 /* Needs DDSCL_EXCLUSIVE */
452 if(!(cooplevel & DDSCL_EXCLUSIVE) )
454 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
455 LeaveCriticalSection(&ddraw_cs);
456 return DDERR_INVALIDPARAMS;
458 /* Need a HWND
459 if(hwnd == 0)
461 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
462 return DDERR_INVALIDPARAMS;
466 This->cooperative_level &= ~DDSCL_NORMAL;
468 /* Don't override focus windows or private device windows */
469 if( hwnd &&
470 !(This->focuswindow) &&
471 !(This->devicewindow) &&
472 (hwnd != window) )
474 This->dest_window = hwnd;
477 else if(cooplevel & DDSCL_EXCLUSIVE)
479 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
480 LeaveCriticalSection(&ddraw_cs);
481 return DDERR_INVALIDPARAMS;
484 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
486 /* Don't create a device window if a focus window is set */
487 if( !(This->focuswindow) )
489 HWND devicewindow = CreateWindowExA(0, This->classname, "DDraw device window",
490 WS_POPUP, 0, 0,
491 GetSystemMetrics(SM_CXSCREEN),
492 GetSystemMetrics(SM_CYSCREEN),
493 NULL, NULL, GetModuleHandleA(0), NULL);
495 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
496 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
498 This->devicewindow = devicewindow;
499 This->dest_window = devicewindow;
503 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
505 /* Enable thread safety in wined3d */
506 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
509 /* Unhandled flags */
510 if(cooplevel & DDSCL_ALLOWREBOOT)
511 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
512 if(cooplevel & DDSCL_ALLOWMODEX)
513 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
514 if(cooplevel & DDSCL_FPUSETUP)
515 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
517 /* Store the cooperative_level */
518 This->cooperative_level |= cooplevel;
519 TRACE("SetCooperativeLevel retuning DD_OK\n");
520 LeaveCriticalSection(&ddraw_cs);
521 return DD_OK;
524 /*****************************************************************************
526 * Helper function for SetDisplayMode and RestoreDisplayMode
528 * Implements DirectDraw's SetDisplayMode, but ignores the value of
529 * ForceRefreshRate, since it is already handled by
530 * IDirectDrawImpl_SetDisplayMode. RestoreDisplayMode can use this function
531 * without worrying that ForceRefreshRate will override the refresh rate. For
532 * argument and return value documentation, see
533 * IDirectDrawImpl_SetDisplayMode.
535 *****************************************************************************/
536 static HRESULT
537 IDirectDrawImpl_SetDisplayModeNoOverride(IDirectDraw7 *iface,
538 DWORD Width,
539 DWORD Height,
540 DWORD BPP,
541 DWORD RefreshRate,
542 DWORD Flags)
544 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
545 WINED3DDISPLAYMODE Mode;
546 HRESULT hr;
547 TRACE("(%p)->(%d,%d,%d,%d,%x): Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
549 EnterCriticalSection(&ddraw_cs);
550 if( !Width || !Height )
552 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
553 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
554 LeaveCriticalSection(&ddraw_cs);
555 return DD_OK;
558 /* Check the exclusive mode
559 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
560 return DDERR_NOEXCLUSIVEMODE;
561 * This is WRONG. Don't know if the SDK is completely
562 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
563 * is returned, but Half-Life 1.1.1.1 (Steam version)
564 * depends on this
567 Mode.Width = Width;
568 Mode.Height = Height;
569 Mode.RefreshRate = RefreshRate;
570 switch(BPP)
572 case 8: Mode.Format = WINED3DFMT_P8; break;
573 case 15: Mode.Format = WINED3DFMT_X1R5G5B5; break;
574 case 16: Mode.Format = WINED3DFMT_R5G6B5; break;
575 case 24: Mode.Format = WINED3DFMT_R8G8B8; break;
576 case 32: Mode.Format = WINED3DFMT_X8R8G8B8; break;
579 /* TODO: The possible return values from msdn suggest that
580 * the screen mode can't be changed if a surface is locked
581 * or some drawing is in progress
584 /* TODO: Lose the primary surface */
585 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
586 0, /* First swapchain */
587 &Mode);
588 LeaveCriticalSection(&ddraw_cs);
589 switch(hr)
591 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
592 default: return hr;
596 /*****************************************************************************
597 * IDirectDraw7::SetDisplayMode
599 * Sets the display screen resolution, color depth and refresh frequency
600 * when in fullscreen mode (in theory).
601 * Possible return values listed in the SDK suggest that this method fails
602 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
603 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
604 * It seems to be valid to pass 0 for With and Height, this has to be tested
605 * It could mean that the current video mode should be left as-is. (But why
606 * call it then?)
608 * Params:
609 * Height, Width: Screen dimension
610 * BPP: Color depth in Bits per pixel
611 * Refreshrate: Screen refresh rate
612 * Flags: Other stuff
614 * Returns
615 * DD_OK on success
617 *****************************************************************************/
618 static HRESULT WINAPI
619 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
620 DWORD Width,
621 DWORD Height,
622 DWORD BPP,
623 DWORD RefreshRate,
624 DWORD Flags)
626 if (force_refresh_rate != 0)
628 TRACE("ForceRefreshRate overriding passed-in refresh rate (%d Hz) to %d Hz\n", RefreshRate, force_refresh_rate);
629 RefreshRate = force_refresh_rate;
632 return IDirectDrawImpl_SetDisplayModeNoOverride(iface, Width, Height, BPP,
633 RefreshRate, Flags);
636 /*****************************************************************************
637 * IDirectDraw7::RestoreDisplayMode
639 * Restores the display mode to what it was at creation time. Basically.
641 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
642 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
643 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
644 * -> DD_1 is released. The screen should be left at 1024x768x32.
645 * -> DD_2 is released. The screen should be set to 1400x1050x32
646 * This case is unhandled right now, but Empire Earth does it this way.
647 * (But perhaps there is something in SetCooperativeLevel to prevent this)
649 * The msdn says that this method resets the display mode to what it was before
650 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
652 * Returns
653 * DD_OK on success
654 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
656 *****************************************************************************/
657 static HRESULT WINAPI
658 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
660 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
661 TRACE("(%p)\n", This);
663 return IDirectDrawImpl_SetDisplayModeNoOverride(ICOM_INTERFACE(This, IDirectDraw7),
664 This->orig_width,
665 This->orig_height,
666 This->orig_bpp,
671 /*****************************************************************************
672 * IDirectDraw7::GetCaps
674 * Returns the drives capabilities
676 * Used for version 1, 2, 4 and 7
678 * Params:
679 * DriverCaps: Structure to write the Hardware accelerated caps to
680 * HelCaps: Structure to write the emulation caps to
682 * Returns
683 * This implementation returns DD_OK only
685 *****************************************************************************/
686 static HRESULT WINAPI
687 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
688 DDCAPS *DriverCaps,
689 DDCAPS *HELCaps)
691 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
692 DDCAPS caps;
693 WINED3DCAPS winecaps;
694 HRESULT hr;
695 DDSCAPS2 ddscaps = {0, 0, 0, 0};
696 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
698 /* One structure must be != NULL */
699 if( (!DriverCaps) && (!HELCaps) )
701 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
702 return DDERR_INVALIDPARAMS;
705 memset(&caps, 0, sizeof(caps));
706 memset(&winecaps, 0, sizeof(winecaps));
707 caps.dwSize = sizeof(caps);
708 EnterCriticalSection(&ddraw_cs);
709 hr = IWineD3DDevice_GetDeviceCaps(This->wineD3DDevice, &winecaps);
710 if(FAILED(hr)) {
711 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
712 LeaveCriticalSection(&ddraw_cs);
713 return hr;
716 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
717 LeaveCriticalSection(&ddraw_cs);
718 if(FAILED(hr)) {
719 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
720 return hr;
723 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
724 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
725 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
726 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
727 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
728 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
729 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
730 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
731 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
732 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
733 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
734 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
735 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
736 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
737 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
739 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
740 * not to use it
742 if(DefaultSurfaceType == SURFACE_GDI) {
743 caps.dwCaps &= ~DDCAPS_3D;
744 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
746 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
747 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
748 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
751 if(DriverCaps)
753 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
754 if (TRACE_ON(ddraw))
756 TRACE("Driver Caps :\n");
757 DDRAW_dump_DDCAPS(DriverCaps);
761 if(HELCaps)
763 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
764 if (TRACE_ON(ddraw))
766 TRACE("HEL Caps :\n");
767 DDRAW_dump_DDCAPS(HELCaps);
771 return DD_OK;
774 /*****************************************************************************
775 * IDirectDraw7::Compact
777 * No idea what it does, MSDN says it's not implemented.
779 * Returns
780 * DD_OK, but this is unchecked
782 *****************************************************************************/
783 static HRESULT WINAPI
784 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
786 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
787 TRACE("(%p)\n", This);
789 return DD_OK;
792 /*****************************************************************************
793 * IDirectDraw7::GetDisplayMode
795 * Returns information about the current display mode
797 * Exists in Version 1, 2, 4 and 7
799 * Params:
800 * DDSD: Address of a surface description structure to write the info to
802 * Returns
803 * DD_OK
805 *****************************************************************************/
806 static HRESULT WINAPI
807 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
808 DDSURFACEDESC2 *DDSD)
810 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
811 HRESULT hr;
812 WINED3DDISPLAYMODE Mode;
813 DWORD Size;
814 TRACE("(%p)->(%p): Relay\n", This, DDSD);
816 EnterCriticalSection(&ddraw_cs);
817 /* This seems sane */
818 if(!DDSD)
820 LeaveCriticalSection(&ddraw_cs);
821 return DDERR_INVALIDPARAMS;
824 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
825 * so one method can be used for all versions (Hopefully)
827 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
828 0 /* swapchain 0 */,
829 &Mode);
830 if( hr != D3D_OK )
832 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
833 LeaveCriticalSection(&ddraw_cs);
834 return hr;
837 Size = DDSD->dwSize;
838 memset(DDSD, 0, Size);
840 DDSD->dwSize = Size;
841 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
842 DDSD->dwWidth = Mode.Width;
843 DDSD->dwHeight = Mode.Height;
844 DDSD->u2.dwRefreshRate = 60;
845 DDSD->ddsCaps.dwCaps = 0;
846 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
847 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
848 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
850 if(TRACE_ON(ddraw))
852 TRACE("Returning surface desc :\n");
853 DDRAW_dump_surface_desc(DDSD);
856 LeaveCriticalSection(&ddraw_cs);
857 return DD_OK;
860 /*****************************************************************************
861 * IDirectDraw7::GetFourCCCodes
863 * Returns an array of supported FourCC codes.
865 * Exists in Version 1, 2, 4 and 7
867 * Params:
868 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
869 * of enumerated codes
870 * Codes: Pointer to an array of DWORDs where the supported codes are written
871 * to
873 * Returns
874 * Always returns DD_OK, as it's a stub for now
876 *****************************************************************************/
877 static HRESULT WINAPI
878 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
879 DWORD *NumCodes, DWORD *Codes)
881 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
882 WINED3DFORMAT formats[] = {
883 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
884 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
885 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
887 DWORD count = 0, i, outsize;
888 HRESULT hr;
889 WINED3DDISPLAYMODE d3ddm;
890 WINED3DSURFTYPE type = This->ImplType;
891 TRACE("(%p)->(%p, %p)\n", This, NumCodes, Codes);
893 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
894 0 /* swapchain 0 */,
895 &d3ddm);
897 outsize = NumCodes && Codes ? *NumCodes : 0;
899 if(type == SURFACE_UNKNOWN) type = SURFACE_GDI;
901 for(i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
902 hr = IWineD3D_CheckDeviceFormat(This->wineD3D,
903 WINED3DADAPTER_DEFAULT,
904 WINED3DDEVTYPE_HAL,
905 d3ddm.Format /* AdapterFormat */,
906 0 /* usage */,
907 WINED3DRTYPE_SURFACE,
908 formats[i],
909 type);
910 if(SUCCEEDED(hr)) {
911 if(count < outsize) {
912 Codes[count] = formats[i];
914 count++;
917 if(NumCodes) {
918 TRACE("Returning %u FourCC codes\n", count);
919 *NumCodes = count;
922 return DD_OK;
925 /*****************************************************************************
926 * IDirectDraw7::GetMonitorFrequency
928 * Returns the monitor's frequency
930 * Exists in Version 1, 2, 4 and 7
932 * Params:
933 * Freq: Pointer to a DWORD to write the frequency to
935 * Returns
936 * Always returns DD_OK
938 *****************************************************************************/
939 static HRESULT WINAPI
940 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
941 DWORD *Freq)
943 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
944 TRACE("(%p)->(%p)\n", This, Freq);
946 /* Ideally this should be in WineD3D, as it concerns the screen setup,
947 * but for now this should make the games happy
949 *Freq = 60;
950 return DD_OK;
953 /*****************************************************************************
954 * IDirectDraw7::GetVerticalBlankStatus
956 * Returns the Vertical blank status of the monitor. This should be in WineD3D
957 * too basically, but as it's a semi stub, I didn't create a function there
959 * Params:
960 * status: Pointer to a BOOL to be filled with the vertical blank status
962 * Returns
963 * DD_OK on success
964 * DDERR_INVALIDPARAMS if status is NULL
966 *****************************************************************************/
967 static HRESULT WINAPI
968 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
969 BOOL *status)
971 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
972 TRACE("(%p)->(%p)\n", This, status);
974 /* This looks sane, the MSDN suggests it too */
975 EnterCriticalSection(&ddraw_cs);
976 if(!status)
978 LeaveCriticalSection(&ddraw_cs);
979 return DDERR_INVALIDPARAMS;
982 *status = This->fake_vblank;
983 This->fake_vblank = !This->fake_vblank;
984 LeaveCriticalSection(&ddraw_cs);
985 return DD_OK;
988 /*****************************************************************************
989 * IDirectDraw7::GetAvailableVidMem
991 * Returns the total and free video memory
993 * Params:
994 * Caps: Specifies the memory type asked for
995 * total: Pointer to a DWORD to be filled with the total memory
996 * free: Pointer to a DWORD to be filled with the free memory
998 * Returns
999 * DD_OK on success
1000 * DDERR_INVALIDPARAMS of free and total are NULL
1002 *****************************************************************************/
1003 static HRESULT WINAPI
1004 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
1006 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1007 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
1009 if(TRACE_ON(ddraw))
1011 TRACE("(%p) Asked for memory with description: ", This);
1012 DDRAW_dump_DDSCAPS2(Caps);
1014 EnterCriticalSection(&ddraw_cs);
1016 /* Todo: System memory vs local video memory vs non-local video memory
1017 * The MSDN also mentions differences between texture memory and other
1018 * resources, but that's not important
1021 if( (!total) && (!free) )
1023 LeaveCriticalSection(&ddraw_cs);
1024 return DDERR_INVALIDPARAMS;
1027 if(total) *total = This->total_vidmem;
1028 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
1030 LeaveCriticalSection(&ddraw_cs);
1031 return DD_OK;
1034 /*****************************************************************************
1035 * IDirectDraw7::Initialize
1037 * Initializes a DirectDraw interface.
1039 * Params:
1040 * GUID: Interface identifier. Well, don't know what this is really good
1041 * for
1043 * Returns
1044 * Returns DD_OK on the first call,
1045 * DDERR_ALREADYINITIALIZED on repeated calls
1047 *****************************************************************************/
1048 static HRESULT WINAPI
1049 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
1050 GUID *Guid)
1052 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1053 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
1055 if(This->initialized)
1057 return DDERR_ALREADYINITIALIZED;
1059 else
1061 return DD_OK;
1065 /*****************************************************************************
1066 * IDirectDraw7::FlipToGDISurface
1068 * "Makes the surface that the GDI writes to the primary surface"
1069 * Looks like some windows specific thing we don't have to care about.
1070 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1071 * show error boxes ;)
1072 * Well, just return DD_OK.
1074 * Returns:
1075 * Always returns DD_OK
1077 *****************************************************************************/
1078 static HRESULT WINAPI
1079 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1081 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1082 TRACE("(%p)\n", This);
1084 return DD_OK;
1087 /*****************************************************************************
1088 * IDirectDraw7::WaitForVerticalBlank
1090 * This method allows applications to get in sync with the vertical blank
1091 * interval.
1092 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1093 * redraw the screen, most likely because of this stub
1095 * Parameters:
1096 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1097 * or DDWAITVB_BLOCKEND
1098 * h: Not used, according to MSDN
1100 * Returns:
1101 * Always returns DD_OK
1103 *****************************************************************************/
1104 static HRESULT WINAPI
1105 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1106 DWORD Flags,
1107 HANDLE h)
1109 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1110 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1112 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1113 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1114 return DDERR_UNSUPPORTED; /* unchecked */
1116 return DD_OK;
1119 /*****************************************************************************
1120 * IDirectDraw7::GetScanLine
1122 * Returns the scan line that is being drawn on the monitor
1124 * Parameters:
1125 * Scanline: Address to write the scan line value to
1127 * Returns:
1128 * Always returns DD_OK
1130 *****************************************************************************/
1131 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1133 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1134 static BOOL hide = FALSE;
1135 WINED3DDISPLAYMODE Mode;
1137 /* This function is called often, so print the fixme only once */
1138 EnterCriticalSection(&ddraw_cs);
1139 if(!hide)
1141 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1142 hide = TRUE;
1145 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1147 &Mode);
1149 /* Fake the line sweeping of the monitor */
1150 /* FIXME: We should synchronize with a source to keep the refresh rate */
1151 *Scanline = This->cur_scanline++;
1152 /* Assume 20 scan lines in the vertical blank */
1153 if (This->cur_scanline >= Mode.Height + 20)
1154 This->cur_scanline = 0;
1156 LeaveCriticalSection(&ddraw_cs);
1157 return DD_OK;
1160 /*****************************************************************************
1161 * IDirectDraw7::TestCooperativeLevel
1163 * Informs the application about the state of the video adapter, depending
1164 * on the cooperative level
1166 * Returns:
1167 * DD_OK if the device is in a sane state
1168 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1169 * if the state is not correct(See below)
1171 *****************************************************************************/
1172 static HRESULT WINAPI
1173 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1175 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1176 HRESULT hr;
1177 TRACE("(%p)\n", This);
1179 EnterCriticalSection(&ddraw_cs);
1180 /* Description from MSDN:
1181 * For fullscreen apps return DDERR_NOEXCLUSIVEMODE if the user switched
1182 * away from the app with e.g. alt-tab. Windowed apps receive
1183 * DDERR_EXCLUSIVEMODEALREADYSET if another application created a
1184 * DirectDraw object in exclusive mode. DDERR_WRONGMODE is returned,
1185 * when the video mode has changed
1188 hr = IWineD3DDevice_TestCooperativeLevel(This->wineD3DDevice);
1190 /* Fix the result value. These values are mapped from their
1191 * d3d9 counterpart.
1193 switch(hr)
1195 case WINED3DERR_DEVICELOST:
1196 if(This->cooperative_level & DDSCL_EXCLUSIVE)
1198 LeaveCriticalSection(&ddraw_cs);
1199 return DDERR_NOEXCLUSIVEMODE;
1201 else
1203 LeaveCriticalSection(&ddraw_cs);
1204 return DDERR_EXCLUSIVEMODEALREADYSET;
1207 case WINED3DERR_DEVICENOTRESET:
1208 LeaveCriticalSection(&ddraw_cs);
1209 return DD_OK;
1211 case WINED3D_OK:
1212 LeaveCriticalSection(&ddraw_cs);
1213 return DD_OK;
1215 case WINED3DERR_DRIVERINTERNALERROR:
1216 default:
1217 ERR("(%p) Unexpected return value %08x from wineD3D, "
1218 " returning DD_OK\n", This, hr);
1221 LeaveCriticalSection(&ddraw_cs);
1222 return DD_OK;
1225 /*****************************************************************************
1226 * IDirectDraw7::GetGDISurface
1228 * Returns the surface that GDI is treating as the primary surface.
1229 * For Wine this is the front buffer
1231 * Params:
1232 * GDISurface: Address to write the surface pointer to
1234 * Returns:
1235 * DD_OK if the surface was found
1236 * DDERR_NOTFOUND if the GDI surface wasn't found
1238 *****************************************************************************/
1239 static HRESULT WINAPI
1240 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1241 IDirectDrawSurface7 **GDISurface)
1243 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1244 IWineD3DSurface *Surf;
1245 IDirectDrawSurface7 *ddsurf;
1246 HRESULT hr;
1247 DDSCAPS2 ddsCaps;
1248 TRACE("(%p)->(%p)\n", This, GDISurface);
1250 /* Get the back buffer from the wineD3DDevice and search its
1251 * attached surfaces for the front buffer
1253 EnterCriticalSection(&ddraw_cs);
1254 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1255 0, /* SwapChain */
1256 0, /* first back buffer*/
1257 WINED3DBACKBUFFER_TYPE_MONO,
1258 &Surf);
1260 if( (hr != D3D_OK) ||
1261 (!Surf) )
1263 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1264 LeaveCriticalSection(&ddraw_cs);
1265 return DDERR_NOTFOUND;
1268 /* GetBackBuffer AddRef()ed the surface, release it */
1269 IWineD3DSurface_Release(Surf);
1271 IWineD3DSurface_GetParent(Surf,
1272 (IUnknown **) &ddsurf);
1273 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1275 /* Find the front buffer */
1276 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1277 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1278 &ddsCaps,
1279 GDISurface);
1280 if(hr != DD_OK)
1282 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1285 /* The AddRef is OK this time */
1286 LeaveCriticalSection(&ddraw_cs);
1287 return hr;
1290 /*****************************************************************************
1291 * IDirectDraw7::EnumDisplayModes
1293 * Enumerates the supported Display modes. The modes can be filtered with
1294 * the DDSD parameter.
1296 * Params:
1297 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1298 * DDSD: Surface description to filter the modes
1299 * Context: Pointer passed back to the callback function
1300 * cb: Application-provided callback function
1302 * Returns:
1303 * DD_OK on success
1304 * DDERR_INVALIDPARAMS if the callback wasn't set
1306 *****************************************************************************/
1307 static HRESULT WINAPI
1308 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1309 DWORD Flags,
1310 DDSURFACEDESC2 *DDSD,
1311 void *Context,
1312 LPDDENUMMODESCALLBACK2 cb)
1314 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1315 unsigned int modenum, fmt;
1316 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1317 WINED3DDISPLAYMODE mode;
1318 DDSURFACEDESC2 callback_sd;
1319 WINED3DDISPLAYMODE *enum_modes = NULL;
1320 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1322 WINED3DFORMAT checkFormatList[] =
1324 WINED3DFMT_R8G8B8,
1325 WINED3DFMT_A8R8G8B8,
1326 WINED3DFMT_X8R8G8B8,
1327 WINED3DFMT_R5G6B5,
1328 WINED3DFMT_X1R5G5B5,
1329 WINED3DFMT_A1R5G5B5,
1330 WINED3DFMT_A4R4G4B4,
1331 WINED3DFMT_R3G3B2,
1332 WINED3DFMT_A8R3G3B2,
1333 WINED3DFMT_X4R4G4B4,
1334 WINED3DFMT_A2B10G10R10,
1335 WINED3DFMT_A8B8G8R8,
1336 WINED3DFMT_X8B8G8R8,
1337 WINED3DFMT_A2R10G10B10,
1338 WINED3DFMT_A8P8,
1339 WINED3DFMT_P8
1342 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1344 EnterCriticalSection(&ddraw_cs);
1345 /* This looks sane */
1346 if(!cb)
1348 LeaveCriticalSection(&ddraw_cs);
1349 return DDERR_INVALIDPARAMS;
1352 if(DDSD)
1354 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1355 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1358 if(!(Flags & DDEDM_REFRESHRATES))
1360 enum_mode_array_size = 16;
1361 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1362 if (!enum_modes)
1364 ERR("Out of memory\n");
1365 LeaveCriticalSection(&ddraw_cs);
1366 return DDERR_OUTOFMEMORY;
1370 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1372 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1374 continue;
1377 modenum = 0;
1378 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1379 WINED3DADAPTER_DEFAULT,
1380 checkFormatList[fmt],
1381 modenum++,
1382 &mode) == WINED3D_OK)
1384 if(DDSD)
1386 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1387 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1390 if(!(Flags & DDEDM_REFRESHRATES))
1392 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1393 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1394 * to be reduced to a single unique result in such case.
1396 BOOL found = FALSE;
1397 unsigned i;
1399 for (i = 0; i < enum_mode_count; i++)
1401 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1402 enum_modes[i].Format == mode.Format)
1404 found = TRUE;
1405 break;
1409 if(found) continue;
1412 memset(&callback_sd, 0, sizeof(callback_sd));
1413 callback_sd.dwSize = sizeof(callback_sd);
1414 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1416 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1417 if(Flags & DDEDM_REFRESHRATES)
1419 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1420 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1423 callback_sd.dwWidth = mode.Width;
1424 callback_sd.dwHeight = mode.Height;
1426 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1428 /* Calc pitch and DWORD align like MSDN says */
1429 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1430 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1432 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1433 callback_sd.u2.dwRefreshRate);
1435 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1437 TRACE("Application asked to terminate the enumeration\n");
1438 HeapFree(GetProcessHeap(), 0, enum_modes);
1439 LeaveCriticalSection(&ddraw_cs);
1440 return DD_OK;
1443 if(!(Flags & DDEDM_REFRESHRATES))
1445 if (enum_mode_count == enum_mode_array_size)
1447 WINED3DDISPLAYMODE *new_enum_modes;
1449 enum_mode_array_size *= 2;
1450 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1452 if (!new_enum_modes)
1454 ERR("Out of memory\n");
1455 HeapFree(GetProcessHeap(), 0, enum_modes);
1456 LeaveCriticalSection(&ddraw_cs);
1457 return DDERR_OUTOFMEMORY;
1460 enum_modes = new_enum_modes;
1463 enum_modes[enum_mode_count++] = mode;
1468 TRACE("End of enumeration\n");
1469 HeapFree(GetProcessHeap(), 0, enum_modes);
1470 LeaveCriticalSection(&ddraw_cs);
1471 return DD_OK;
1474 /*****************************************************************************
1475 * IDirectDraw7::EvaluateMode
1477 * Used with IDirectDraw7::StartModeTest to test video modes.
1478 * EvaluateMode is used to pass or fail a mode, and continue with the next
1479 * mode
1481 * Params:
1482 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1483 * Timeout: Returns the amount of seconds left before the mode would have
1484 * been failed automatically
1486 * Returns:
1487 * This implementation always DD_OK, because it's a stub
1489 *****************************************************************************/
1490 static HRESULT WINAPI
1491 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1492 DWORD Flags,
1493 DWORD *Timeout)
1495 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1496 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1498 /* When implementing this, implement it in WineD3D */
1500 return DD_OK;
1503 /*****************************************************************************
1504 * IDirectDraw7::GetDeviceIdentifier
1506 * Returns the device identifier, which gives information about the driver
1507 * Our device identifier is defined at the beginning of this file.
1509 * Params:
1510 * DDDI: Address for the returned structure
1511 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1513 * Returns:
1514 * On success it returns DD_OK
1515 * DDERR_INVALIDPARAMS if DDDI is NULL
1517 *****************************************************************************/
1518 static HRESULT WINAPI
1519 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1520 DDDEVICEIDENTIFIER2 *DDDI,
1521 DWORD Flags)
1523 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1524 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1526 if(!DDDI)
1527 return DDERR_INVALIDPARAMS;
1529 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1530 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1531 * to any modern hardware, nor is it interesting for Wine, so ignore it
1534 *DDDI = deviceidentifier;
1535 return DD_OK;
1538 /*****************************************************************************
1539 * IDirectDraw7::GetSurfaceFromDC
1541 * Returns the Surface for a GDI device context handle.
1542 * Is this related to IDirectDrawSurface::GetDC ???
1544 * Params:
1545 * hdc: hdc to return the surface for
1546 * Surface: Address to write the surface pointer to
1548 * Returns:
1549 * Always returns DD_OK because it's a stub
1551 *****************************************************************************/
1552 static HRESULT WINAPI
1553 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1554 HDC hdc,
1555 IDirectDrawSurface7 **Surface)
1557 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1558 FIXME("(%p)->(%p,%p): Stub!\n", This, hdc, Surface);
1560 /* Implementation idea if needed: Loop through all surfaces and compare
1561 * their hdc with hdc. Implement it in WineD3D! */
1562 return DDERR_NOTFOUND;
1565 /*****************************************************************************
1566 * IDirectDraw7::RestoreAllSurfaces
1568 * Calls the restore method of all surfaces
1570 * Params:
1572 * Returns:
1573 * Always returns DD_OK because it's a stub
1575 *****************************************************************************/
1576 static HRESULT WINAPI
1577 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1579 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1580 FIXME("(%p): Stub\n", This);
1582 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1583 * get their parent and call their restore method. Do not implement
1584 * it in WineD3D, as restoring a surface means re-creating the
1585 * WineD3DDSurface
1587 return DD_OK;
1590 /*****************************************************************************
1591 * IDirectDraw7::StartModeTest
1593 * Tests the specified video modes to update the system registry with
1594 * refresh rate information. StartModeTest starts the mode test,
1595 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1596 * isn't called within 15 seconds, the mode is failed automatically
1598 * As refresh rates are handled by the X server, I don't think this
1599 * Method is important
1601 * Params:
1602 * Modes: An array of mode specifications
1603 * NumModes: The number of modes in Modes
1604 * Flags: Some flags...
1606 * Returns:
1607 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1608 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1609 * otherwise DD_OK
1611 *****************************************************************************/
1612 static HRESULT WINAPI
1613 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1614 SIZE *Modes,
1615 DWORD NumModes,
1616 DWORD Flags)
1618 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1619 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1621 /* This looks sane */
1622 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1624 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1625 * As it is not, DDERR_TESTFINISHED is returned
1626 * (hopefully that's correct
1628 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1629 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1632 return DD_OK;
1635 /*****************************************************************************
1636 * IDirectDrawImpl_RecreateSurfacesCallback
1638 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1639 * It re-recreates the WineD3DSurface. It's pretty straightforward
1641 *****************************************************************************/
1642 HRESULT WINAPI
1643 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1644 DDSURFACEDESC2 *desc,
1645 void *Context)
1647 IDirectDrawSurfaceImpl *surfImpl = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1648 IDirectDrawSurface7,
1649 surf);
1650 IDirectDrawImpl *This = surfImpl->ddraw;
1651 IUnknown *Parent;
1652 IParentImpl *parImpl = NULL;
1653 IWineD3DSurface *wineD3DSurface;
1654 IWineD3DSwapChain *swapchain;
1655 HRESULT hr;
1656 void *tmp;
1657 IWineD3DClipper *clipper = NULL;
1659 WINED3DSURFACE_DESC Desc;
1660 WINED3DFORMAT Format;
1661 WINED3DRESOURCETYPE Type;
1662 DWORD Usage;
1663 WINED3DPOOL Pool;
1664 UINT Size;
1666 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1667 DWORD MultiSampleQuality;
1668 UINT Width;
1669 UINT Height;
1671 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1673 /* For the enumeration */
1674 IDirectDrawSurface7_Release(surf);
1676 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1678 /* Get the objects */
1679 swapchain = surfImpl->wineD3DSwapChain;
1680 surfImpl->wineD3DSwapChain = NULL;
1681 wineD3DSurface = surfImpl->WineD3DSurface;
1682 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1683 IUnknown_Release(Parent); /* For the getParent */
1685 /* Is the parent an IParent interface? */
1686 if(IUnknown_QueryInterface(Parent, &IID_IParent, &tmp) == S_OK)
1688 /* It is a IParent interface! */
1689 IUnknown_Release(Parent); /* For the QueryInterface */
1690 parImpl = ICOM_OBJECT(IParentImpl, IParent, Parent);
1691 /* Release the reference the parent interface is holding */
1692 IWineD3DSurface_Release(wineD3DSurface);
1695 /* get the clipper */
1696 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1698 /* Get the surface properties */
1699 Desc.Format = &Format;
1700 Desc.Type = &Type;
1701 Desc.Usage = &Usage;
1702 Desc.Pool = &Pool;
1703 Desc.Size = &Size;
1704 Desc.MultiSampleType = &MultiSampleType;
1705 Desc.MultiSampleQuality = &MultiSampleQuality;
1706 Desc.Width = &Width;
1707 Desc.Height = &Height;
1709 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1710 if(hr != D3D_OK) return hr;
1712 if(swapchain) {
1713 /* If there's a swapchain, it owns the IParent interface. Create a new one for the
1714 * new surface
1716 parImpl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*parImpl));
1717 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
1718 parImpl->ref = 1;
1720 Parent = (IUnknown *) parImpl;
1723 /* Create the new surface */
1724 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
1725 Width, Height, Format,
1726 TRUE /* Lockable */,
1727 FALSE /* Discard */,
1728 surfImpl->mipmap_level,
1729 &surfImpl->WineD3DSurface,
1730 Type,
1731 Usage,
1732 Pool,
1733 MultiSampleType,
1734 MultiSampleQuality,
1735 0 /* SharedHandle */,
1736 This->ImplType,
1737 Parent);
1739 if(hr != D3D_OK)
1740 return hr;
1742 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1744 /* Update the IParent if it exists */
1745 if(parImpl)
1747 parImpl->child = (IUnknown *) surfImpl->WineD3DSurface;
1748 /* Add a reference for the IParent */
1749 IWineD3DSurface_AddRef(surfImpl->WineD3DSurface);
1751 /* TODO: Copy the surface content, except for render targets */
1753 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
1754 * the swapchain
1756 if(swapchain) {
1757 /* The backbuffers have the swapchain set as well, but the primary
1758 * owns it and destroys it
1760 if(surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
1761 IWineD3DDevice_UninitGDI(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1763 surfImpl->isRenderTarget = FALSE;
1764 } else {
1765 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1766 TRACE("Surface released successful, next surface\n");
1767 else
1768 ERR("Something's still holding the old WineD3DSurface\n");
1771 surfImpl->ImplType = This->ImplType;
1773 if(clipper)
1775 IWineD3DClipper_Release(clipper);
1777 return DDENUMRET_OK;
1780 /*****************************************************************************
1781 * IDirectDrawImpl_RecreateAllSurfaces
1783 * A function, that converts all wineD3DSurfaces to the new implementation type
1784 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1785 * new WineD3DSurface, copies the content and releases the old surface
1787 *****************************************************************************/
1788 static HRESULT
1789 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1791 DDSURFACEDESC2 desc;
1792 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1794 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1796 /* Should happen almost never */
1797 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1798 /* Shutdown d3d */
1799 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroyDepthStencilSurface, D3D7CB_DestroySwapChain);
1801 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1803 memset(&desc, 0, sizeof(desc));
1804 desc.dwSize = sizeof(desc);
1806 return IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(This, IDirectDraw7),
1808 &desc,
1809 This,
1810 IDirectDrawImpl_RecreateSurfacesCallback);
1813 /*****************************************************************************
1814 * D3D7CB_CreateSurface
1816 * Callback function for IDirect3DDevice_CreateTexture. It searches for the
1817 * correct mipmap sublevel, and returns it to WineD3D.
1818 * The surfaces are created already by IDirectDraw7::CreateSurface
1820 * Params:
1821 * With, Height: With and height of the surface
1822 * Format: The requested format
1823 * Usage, Pool: D3DUSAGE and D3DPOOL of the surface
1824 * level: The mipmap level
1825 * Face: The cube map face type
1826 * Surface: Pointer to pass the created surface back at
1827 * SharedHandle: NULL
1829 * Returns:
1830 * D3D_OK
1832 *****************************************************************************/
1833 static HRESULT WINAPI
1834 D3D7CB_CreateSurface(IUnknown *device,
1835 IUnknown *pSuperior,
1836 UINT Width, UINT Height,
1837 WINED3DFORMAT Format,
1838 DWORD Usage, WINED3DPOOL Pool, UINT level,
1839 WINED3DCUBEMAP_FACES Face,
1840 IWineD3DSurface **Surface,
1841 HANDLE *SharedHandle)
1843 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
1844 IDirectDrawSurfaceImpl *surf = NULL;
1845 UINT i = 0;
1846 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
1847 TRACE("(%p) call back. surf=%p. Face %d level %d\n", device, This->tex_root, Face, level);
1849 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
1850 switch(Face)
1852 case WINED3DCUBEMAP_FACE_POSITIVE_X:
1853 TRACE("Asked for positive x\n");
1854 if(searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP) {
1855 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
1857 surf = This->tex_root; break;
1858 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
1859 TRACE("Asked for negative x\n");
1860 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
1861 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
1862 TRACE("Asked for positive y\n");
1863 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
1864 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
1865 TRACE("Asked for negative y\n");
1866 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
1867 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
1868 TRACE("Asked for positive z\n");
1869 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
1870 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
1871 TRACE("Asked for negative z\n");
1872 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
1873 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
1876 if(!surf)
1878 IDirectDrawSurface7 *attached;
1879 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This->tex_root, IDirectDrawSurface7),
1880 &searchcaps,
1881 &attached);
1882 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1883 IDirectDrawSurface7_Release(attached);
1885 if(!surf) ERR("root search surface not found\n");
1887 /* Find the wanted mipmap. There are enough mipmaps in the chain */
1888 while(i < level)
1890 IDirectDrawSurface7 *attached;
1891 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(surf, IDirectDrawSurface7),
1892 &searchcaps,
1893 &attached);
1894 if(!attached) ERR("Surface not found\n");
1895 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1896 IDirectDrawSurface7_Release(attached);
1897 i++;
1900 /* Return the surface */
1901 *Surface = surf->WineD3DSurface;
1903 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *Surface, surf);
1904 return D3D_OK;
1907 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1908 IUnknown* swapChainParent;
1909 TRACE("(%p) call back\n", pSwapChain);
1911 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1912 IUnknown_Release(swapChainParent);
1913 return IUnknown_Release(swapChainParent);
1916 ULONG WINAPI D3D7CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
1917 IUnknown* surfaceParent;
1918 TRACE("(%p) call back\n", pSurface);
1920 IWineD3DSurface_GetParent(pSurface, &surfaceParent);
1921 IUnknown_Release(surfaceParent);
1922 return IUnknown_Release(surfaceParent);
1925 /*****************************************************************************
1926 * IDirectDrawImpl_CreateNewSurface
1928 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1929 * with the passed parameters.
1931 * Params:
1932 * DDSD: Description of the surface to create
1933 * Surf: Address to store the interface pointer at
1935 * Returns:
1936 * DD_OK on success
1938 *****************************************************************************/
1939 static HRESULT
1940 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1941 DDSURFACEDESC2 *pDDSD,
1942 IDirectDrawSurfaceImpl **ppSurf,
1943 UINT level)
1945 HRESULT hr;
1946 UINT Width = 0, Height = 0;
1947 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1948 WINED3DRESOURCETYPE ResType = WINED3DRTYPE_SURFACE;
1949 DWORD Usage = 0;
1950 WINED3DSURFTYPE ImplType = This->ImplType;
1951 WINED3DSURFACE_DESC Desc;
1952 IUnknown *Parent;
1953 IParentImpl *parImpl = NULL;
1954 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1956 /* Dummies for GetDesc */
1957 WINED3DPOOL dummy_d3dpool;
1958 WINED3DMULTISAMPLE_TYPE dummy_mst;
1959 UINT dummy_uint;
1960 DWORD dummy_dword;
1962 if (TRACE_ON(ddraw))
1964 TRACE(" (%p) Requesting surface desc :\n", This);
1965 DDRAW_dump_surface_desc(pDDSD);
1968 /* Select the surface type, if it wasn't choosen yet */
1969 if(ImplType == SURFACE_UNKNOWN)
1971 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1972 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1974 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1975 ImplType = SURFACE_OPENGL;
1978 /* Otherwise use GDI surfaces for now */
1979 else
1981 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1982 ImplType = SURFACE_GDI;
1985 /* Policy if all surface implementations are available:
1986 * First, check if a default type was set with winecfg. If not,
1987 * try Xrender surfaces, and use them if they work. Next, check if
1988 * accelerated OpenGL is available, and use GL surfaces in this
1989 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1990 * was created, always use OpenGL surfaces.
1992 * (Note: Xrender surfaces are not implemented for now, the
1993 * unaccelerated implementation uses GDI to render in Software)
1996 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1997 * be re-created. This could be done with IDirectDrawSurface7::Restore
1999 This->ImplType = ImplType;
2001 else
2003 if((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE ) &&
2004 (This->ImplType != SURFACE_OPENGL ) && DefaultSurfaceType == SURFACE_UNKNOWN)
2006 /* We have to change to OpenGL,
2007 * and re-create all WineD3DSurfaces
2009 ImplType = SURFACE_OPENGL;
2010 This->ImplType = ImplType;
2011 TRACE("(%p) Re-creating all surfaces\n", This);
2012 IDirectDrawImpl_RecreateAllSurfaces(This);
2013 TRACE("(%p) Done recreating all surfaces\n", This);
2015 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2017 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
2018 /* Do not fail surface creation, only fail 3D device creation */
2022 if (!(pDDSD->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)) &&
2023 !((pDDSD->ddsCaps.dwCaps & DDSCAPS_TEXTURE) && (pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)) )
2025 /* Tests show surfaces without memory flags get these flags added right after creation. */
2026 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
2028 /* Get the correct wined3d usage */
2029 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
2030 DDSCAPS_3DDEVICE ) )
2032 Usage |= WINED3DUSAGE_RENDERTARGET;
2034 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
2036 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
2038 Usage |= WINED3DUSAGE_OVERLAY;
2040 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
2042 /* The depth stencil creation callback sets this flag.
2043 * Set the WineD3D usage to let it know that it's a depth
2044 * Stencil surface.
2046 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
2048 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2050 Pool = WINED3DPOOL_SYSTEMMEM;
2052 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
2054 Pool = WINED3DPOOL_MANAGED;
2055 /* Managed textures have the system memory flag set */
2056 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
2058 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
2060 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
2061 * and texturemanage
2063 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
2066 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
2067 if(Format == WINED3DFMT_UNKNOWN)
2069 ERR("Unsupported / Unknown pixelformat\n");
2070 return DDERR_INVALIDPIXELFORMAT;
2073 /* Create the Surface object */
2074 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
2075 if(!*ppSurf)
2077 ERR("(%p) Error allocating memory for a surface\n", This);
2078 return DDERR_OUTOFVIDEOMEMORY;
2080 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface7, IDirectDrawSurface7_Vtbl);
2081 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface3, IDirectDrawSurface3_Vtbl);
2082 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawGammaControl, IDirectDrawGammaControl_Vtbl);
2083 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture2, IDirect3DTexture2_Vtbl);
2084 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture, IDirect3DTexture1_Vtbl);
2085 (*ppSurf)->ref = 1;
2086 (*ppSurf)->version = 7;
2087 TRACE("%p->version = %d\n", (*ppSurf), (*ppSurf)->version);
2088 (*ppSurf)->ddraw = This;
2089 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
2090 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2091 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
2093 /* Surface attachments */
2094 (*ppSurf)->next_attached = NULL;
2095 (*ppSurf)->first_attached = *ppSurf;
2097 /* Needed to re-create the surface on an implementation change */
2098 (*ppSurf)->ImplType = ImplType;
2100 /* For D3DDevice creation */
2101 (*ppSurf)->isRenderTarget = FALSE;
2103 /* A trace message for debugging */
2104 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
2106 if(pDDSD->ddsCaps.dwCaps & ( DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE) )
2108 /* Render targets and textures need a IParent interface,
2109 * because WineD3D will destroy them when the swapchain
2110 * is released
2112 parImpl = HeapAlloc(GetProcessHeap(), 0, sizeof(IParentImpl));
2113 if(!parImpl)
2115 ERR("Out of memory when allocating memory for a IParent implementation\n");
2116 return DDERR_OUTOFMEMORY;
2118 parImpl->ref = 1;
2119 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
2120 Parent = (IUnknown *) ICOM_INTERFACE(parImpl, IParent);
2121 TRACE("Using IParent interface %p as parent\n", parImpl);
2123 else
2125 /* Use the surface as parent */
2126 Parent = (IUnknown *) ICOM_INTERFACE(*ppSurf, IDirectDrawSurface7);
2127 TRACE("Using Surface interface %p as parent\n", *ppSurf);
2130 /* Now create the WineD3D Surface */
2131 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
2132 pDDSD->dwWidth,
2133 pDDSD->dwHeight,
2134 Format,
2135 TRUE /* Lockable */,
2136 FALSE /* Discard */,
2137 level,
2138 &(*ppSurf)->WineD3DSurface,
2139 ResType, Usage,
2140 Pool,
2141 WINED3DMULTISAMPLE_NONE,
2142 0 /* MultiSampleQuality */,
2143 0 /* SharedHandle */,
2144 ImplType,
2145 Parent);
2147 if(hr != D3D_OK)
2149 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
2150 return hr;
2153 /* Set the child of the parent implementation if it exists */
2154 if(parImpl)
2156 parImpl->child = (IUnknown *) (*ppSurf)->WineD3DSurface;
2157 /* The IParent releases the WineD3DSurface, and
2158 * the ddraw surface does that too. Hold a reference
2160 IWineD3DSurface_AddRef((*ppSurf)->WineD3DSurface);
2163 /* Increase the surface counter, and attach the surface */
2164 InterlockedIncrement(&This->surfaces);
2165 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2167 /* Here we could store all created surfaces in the DirectDrawImpl structure,
2168 * But this could also be delegated to WineDDraw, as it keeps track of all its
2169 * resources. Not implemented for now, as there are more important things ;)
2172 /* Get the pixel format of the WineD3DSurface and store it.
2173 * Don't use the Format choosen above, WineD3D might have
2174 * changed it
2176 Desc.Format = &Format;
2177 Desc.Type = &ResType;
2178 Desc.Usage = &Usage;
2179 Desc.Pool = &dummy_d3dpool;
2180 Desc.Size = &dummy_uint;
2181 Desc.MultiSampleType = &dummy_mst;
2182 Desc.MultiSampleQuality = &dummy_dword;
2183 Desc.Width = &Width;
2184 Desc.Height = &Height;
2186 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
2187 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
2188 if(hr != D3D_OK)
2190 ERR("IWineD3DSurface::GetDesc failed\n");
2191 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
2192 return hr;
2195 if(Format == WINED3DFMT_UNKNOWN)
2197 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
2199 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
2201 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
2202 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
2203 * WineD3DDevice might not be usable for 3D yet, so an extra method was created.
2204 * TODO: Test other fourcc formats
2206 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
2207 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
2209 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
2210 if(Format == WINED3DFMT_DXT1)
2212 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
2214 else
2216 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
2219 else
2221 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
2222 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
2225 /* Application passed a color key? Set it! */
2226 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
2228 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2229 DDCKEY_DESTOVERLAY,
2230 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
2232 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
2234 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2235 DDCKEY_DESTBLT,
2236 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
2238 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
2240 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2241 DDCKEY_SRCOVERLAY,
2242 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2244 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2246 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2247 DDCKEY_SRCBLT,
2248 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2250 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2252 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2253 if(hr != WINED3D_OK)
2255 /* No need for a trace here, wined3d does that for us */
2256 IDirectDrawSurface7_Release(ICOM_INTERFACE((*ppSurf), IDirectDrawSurface7));
2257 return hr;
2261 return DD_OK;
2263 /*****************************************************************************
2264 * CreateAdditionalSurfaces
2266 * Creates a new mipmap chain.
2268 * Params:
2269 * root: Root surface to attach the newly created chain to
2270 * count: number of surfaces to create
2271 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2272 * effects on the caller
2273 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2274 * creates an additional surface without the mipmapping flags
2276 *****************************************************************************/
2277 static HRESULT
2278 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2279 IDirectDrawSurfaceImpl *root,
2280 UINT count,
2281 DDSURFACEDESC2 DDSD,
2282 BOOL CubeFaceRoot)
2284 UINT i, j, level = 0;
2285 HRESULT hr;
2286 IDirectDrawSurfaceImpl *last = root;
2288 for(i = 0; i < count; i++)
2290 IDirectDrawSurfaceImpl *object2 = NULL;
2292 /* increase the mipmap level, but only if a mipmap is created
2293 * In this case, also halve the size
2295 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2297 level++;
2298 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2299 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2300 /* Set the mipmap sublevel flag according to msdn */
2301 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2303 else
2305 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2307 CubeFaceRoot = FALSE;
2309 hr = IDirectDrawImpl_CreateNewSurface(This,
2310 &DDSD,
2311 &object2,
2312 level);
2313 if(hr != DD_OK)
2315 return hr;
2318 /* Add the new surface to the complex attachment array */
2319 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2321 if(last->complex_array[j]) continue;
2322 last->complex_array[j] = object2;
2323 break;
2325 last = object2;
2327 /* Remove the (possible) back buffer cap from the new surface description,
2328 * because only one surface in the flipping chain is a back buffer, one
2329 * is a front buffer, the others are just primary surfaces.
2331 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2333 return DD_OK;
2336 /*****************************************************************************
2337 * IDirectDraw7::CreateSurface
2339 * Creates a new IDirectDrawSurface object and returns its interface.
2341 * The surface connections with wined3d are a bit tricky. Basically it works
2342 * like this:
2344 * |------------------------| |-----------------|
2345 * | DDraw surface | | WineD3DSurface |
2346 * | | | |
2347 * | WineD3DSurface |-------------->| |
2348 * | Child |<------------->| Parent |
2349 * |------------------------| |-----------------|
2351 * The DDraw surface is the parent of the wined3d surface, and it releases
2352 * the WineD3DSurface when the ddraw surface is destroyed.
2354 * However, for all surfaces which can be in a container in WineD3D,
2355 * we have to do this. These surfaces are usually complex surfaces,
2356 * so this concerns primary surfaces with a front and a back buffer,
2357 * and textures.
2359 * |------------------------| |-----------------|
2360 * | DDraw surface | | Container |
2361 * | | | |
2362 * | Child |<------------->| Parent |
2363 * | Texture |<------------->| |
2364 * | WineD3DSurface |<----| | Levels |<--|
2365 * | Complex connection | | | | |
2366 * |------------------------| | |-----------------| |
2367 * ^ | |
2368 * | | |
2369 * | | |
2370 * | |------------------| | |-----------------| |
2371 * | | IParent | |-------->| WineD3DSurface | |
2372 * | | | | | |
2373 * | | Child |<------------->| Parent | |
2374 * | | | | Container |<--|
2375 * | |------------------| |-----------------| |
2376 * | |
2377 * | |----------------------| |
2378 * | | DDraw surface 2 | |
2379 * | | | |
2380 * |<->| Complex root Child | |
2381 * | | Texture | |
2382 * | | WineD3DSurface |<----| |
2383 * | |----------------------| | |
2384 * | | |
2385 * | |---------------------| | |-----------------| |
2386 * | | IParent | |----->| WineD3DSurface | |
2387 * | | | | | |
2388 * | | Child |<---------->| Parent | |
2389 * | |---------------------| | Container |<--|
2390 * | |-----------------| |
2391 * | |
2392 * | ---More surfaces can follow--- |
2394 * The reason is that the IWineD3DSwapchain(render target container)
2395 * and the IWineD3DTexure(Texture container) release the parents
2396 * of their surface's children, but by releasing the complex root
2397 * the surfaces which are complexly attached to it are destroyed
2398 * too. See IDirectDrawSurface::Release for a more detailed
2399 * explanation.
2401 * Params:
2402 * DDSD: Description of the surface to create
2403 * Surf: Address to store the interface pointer at
2404 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2405 * aggregation, so it has to be NULL
2407 * Returns:
2408 * DD_OK on success
2409 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2410 * DDERR_* if an error occurs
2412 *****************************************************************************/
2413 static HRESULT WINAPI
2414 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2415 DDSURFACEDESC2 *DDSD,
2416 IDirectDrawSurface7 **Surf,
2417 IUnknown *UnkOuter)
2419 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2420 IDirectDrawSurfaceImpl *object = NULL;
2421 HRESULT hr;
2422 LONG extra_surfaces = 0;
2423 DDSURFACEDESC2 desc2;
2424 WINED3DDISPLAYMODE Mode;
2426 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2428 /* Some checks before we start */
2429 if (TRACE_ON(ddraw))
2431 TRACE(" (%p) Requesting surface desc :\n", This);
2432 DDRAW_dump_surface_desc(DDSD);
2434 EnterCriticalSection(&ddraw_cs);
2436 if (UnkOuter != NULL)
2438 FIXME("(%p) : outer != NULL?\n", This);
2439 LeaveCriticalSection(&ddraw_cs);
2440 return CLASS_E_NOAGGREGATION; /* unchecked */
2443 if (Surf == NULL)
2445 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2446 LeaveCriticalSection(&ddraw_cs);
2447 return E_POINTER; /* unchecked */
2450 if (!(DDSD->dwFlags & DDSD_CAPS))
2452 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2453 DDSD->dwFlags |= DDSD_CAPS;
2456 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2458 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2459 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2462 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2464 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2465 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2466 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2469 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2470 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2472 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2473 *Surf = NULL;
2474 LeaveCriticalSection(&ddraw_cs);
2475 return DDERR_NOEXCLUSIVEMODE;
2478 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2479 WARN("Application tried to create an explicit front or back buffer\n");
2480 LeaveCriticalSection(&ddraw_cs);
2481 return DDERR_INVALIDCAPS;
2483 /* Check cube maps but only if the size includes them */
2484 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2486 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2487 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2489 WARN("Cube map faces requested without cube map flag\n");
2490 LeaveCriticalSection(&ddraw_cs);
2491 return DDERR_INVALIDCAPS;
2493 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2494 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2496 WARN("Cube map without faces requested\n");
2497 LeaveCriticalSection(&ddraw_cs);
2498 return DDERR_INVALIDPARAMS;
2501 /* Quick tests confirm those can be created, but we don't do that yet */
2502 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2503 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2505 FIXME("Partial cube maps not supported yet\n");
2509 /* According to the msdn this flag is ignored by CreateSurface */
2510 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2511 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2513 /* Modify some flags */
2514 memset(&desc2, 0, sizeof(desc2));
2515 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2516 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2517 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2518 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2520 /* Get the video mode from WineD3D - we will need it */
2521 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2522 0, /* Swapchain 0 */
2523 &Mode);
2524 if(FAILED(hr))
2526 ERR("Failed to read display mode from wined3d\n");
2527 switch(This->orig_bpp)
2529 case 8:
2530 Mode.Format = WINED3DFMT_P8;
2531 break;
2533 case 15:
2534 Mode.Format = WINED3DFMT_X1R5G5B5;
2535 break;
2537 case 16:
2538 Mode.Format = WINED3DFMT_R5G6B5;
2539 break;
2541 case 24:
2542 Mode.Format = WINED3DFMT_R8G8B8;
2543 break;
2545 case 32:
2546 Mode.Format = WINED3DFMT_X8R8G8B8;
2547 break;
2549 Mode.Width = This->orig_width;
2550 Mode.Height = This->orig_height;
2553 /* No pixelformat given? Use the current screen format */
2554 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2556 desc2.dwFlags |= DDSD_PIXELFORMAT;
2557 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2559 /* Wait: It could be a Z buffer */
2560 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2562 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2564 case 15:
2565 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D15S1);
2566 break;
2567 case 16:
2568 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16);
2569 break;
2570 case 24:
2571 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D24X8);
2572 break;
2573 case 32:
2574 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32);
2575 break;
2576 default:
2577 ERR("Unknown Z buffer bit depth\n");
2580 else
2582 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2586 /* No Width or no Height? Use the original screen size
2588 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2589 !(desc2.dwFlags & DDSD_HEIGHT) )
2591 /* Invalid for non-render targets */
2592 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2594 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2595 *Surf = NULL;
2596 LeaveCriticalSection(&ddraw_cs);
2597 return DDERR_INVALIDPARAMS;
2600 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2601 desc2.dwWidth = Mode.Width;
2602 desc2.dwHeight = Mode.Height;
2605 /* Mipmap count fixes */
2606 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2608 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2610 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2612 /* Mipmap count is given, should not be 0 */
2613 if( desc2.u2.dwMipMapCount == 0 )
2615 LeaveCriticalSection(&ddraw_cs);
2616 return DDERR_INVALIDPARAMS;
2619 else
2621 /* Undocumented feature: Create sublevels until
2622 * either the width or the height is 1
2624 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2625 desc2.dwWidth : desc2.dwHeight;
2626 desc2.u2.dwMipMapCount = 0;
2627 while( min )
2629 desc2.u2.dwMipMapCount += 1;
2630 min >>= 1;
2634 else
2636 /* Not-complex mipmap -> Mipmapcount = 1 */
2637 desc2.u2.dwMipMapCount = 1;
2639 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2641 /* There's a mipmap count in the created surface in any case */
2642 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2644 /* If no mipmap is given, the texture has only one level */
2646 /* The first surface is a front buffer, the back buffer is created afterwards */
2647 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2649 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2652 /* The root surface in a cube map is positive x */
2653 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2655 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2656 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2659 /* Create the first surface */
2660 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2661 if( hr != DD_OK)
2663 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2664 LeaveCriticalSection(&ddraw_cs);
2665 return hr;
2667 object->is_complex_root = TRUE;
2669 *Surf = ICOM_INTERFACE(object, IDirectDrawSurface7);
2671 /* Create Additional surfaces if necessary
2672 * This applies to Primary surfaces which have a back buffer count
2673 * set, but not to mipmap textures. In case of Mipmap textures,
2674 * wineD3D takes care of the creation of additional surfaces
2676 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2678 extra_surfaces = DDSD->dwBackBufferCount;
2679 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2680 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2683 hr = DD_OK;
2684 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2686 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2687 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2688 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2689 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2690 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2691 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2692 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2693 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2694 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2695 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2696 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2697 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2698 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2699 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2700 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2701 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2702 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2705 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2706 if(hr != DD_OK)
2708 /* This destroys and possibly created surfaces too */
2709 IDirectDrawSurface_Release( ICOM_INTERFACE(object, IDirectDrawSurface7) );
2710 LeaveCriticalSection(&ddraw_cs);
2711 return hr;
2714 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2715 * But attach the d3ddevice only if the currently created surface was
2716 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2717 * The only case I can think of where this doesn't apply is when a
2718 * 2D app was configured by the user to run with OpenGL and it didn't create
2719 * the render target as first surface. In this case the render target creation
2720 * will cause the 3D init.
2722 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2723 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2725 IDirectDrawSurfaceImpl *target = object, *surface;
2726 struct list *entry;
2728 /* Search for the primary to use as render target */
2729 LIST_FOR_EACH(entry, &This->surface_list)
2731 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2732 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2733 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2735 /* found */
2736 target = surface;
2737 TRACE("Using primary %p as render target\n", target);
2738 break;
2742 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2743 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2744 if(hr != D3D_OK)
2746 IDirectDrawSurfaceImpl *release_surf;
2747 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2748 *Surf = NULL;
2750 /* The before created surface structures are in an incomplete state here.
2751 * WineD3D holds the reference on the IParents, and it released them on the failure
2752 * already. So the regular release method implementation would fail on the attempt
2753 * to destroy either the IParents or the swapchain. So free the surface here.
2754 * The surface structure here is a list, not a tree, because onscreen targets
2755 * cannot be cube textures
2757 while(object)
2759 release_surf = object;
2760 object = object->complex_array[0];
2761 IDirectDrawSurfaceImpl_Destroy(release_surf);
2763 LeaveCriticalSection(&ddraw_cs);
2764 return hr;
2766 } else if(!(This->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
2767 IDirectDrawImpl_CreateGDISwapChain(This, object);
2770 /* Addref the ddraw interface to keep an reference for each surface */
2771 IDirectDraw7_AddRef(iface);
2772 object->ifaceToRelease = (IUnknown *) iface;
2774 /* Create a WineD3DTexture if a texture was requested */
2775 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2777 UINT levels;
2778 WINED3DFORMAT Format;
2779 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2781 This->tex_root = object;
2783 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2785 /* a mipmap is created, create enough levels */
2786 levels = desc2.u2.dwMipMapCount;
2788 else
2790 /* No mipmap is created, create one level */
2791 levels = 1;
2794 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2795 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2797 Pool = WINED3DPOOL_SYSTEMMEM;
2799 /* Should I forward the MANAGED cap to the managed pool ? */
2801 /* Get the format. It's set already by CreateNewSurface */
2802 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2804 /* The surfaces are already created, the callback only
2805 * passes the IWineD3DSurface to WineD3D
2807 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2809 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice,
2810 DDSD->dwWidth, /* Edgelength */
2811 levels,
2812 0, /* usage */
2813 Format,
2814 Pool,
2815 (IWineD3DCubeTexture **) &object->wineD3DTexture,
2816 0, /* SharedHandle */
2817 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2818 D3D7CB_CreateSurface);
2820 else
2822 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice,
2823 DDSD->dwWidth, DDSD->dwHeight,
2824 levels, /* MipMapCount = Levels */
2825 0, /* usage */
2826 Format,
2827 Pool,
2828 (IWineD3DTexture **) &object->wineD3DTexture,
2829 0, /* SharedHandle */
2830 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2831 D3D7CB_CreateSurface );
2833 This->tex_root = NULL;
2836 LeaveCriticalSection(&ddraw_cs);
2837 return hr;
2840 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2841 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2843 static BOOL
2844 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2845 const DDPIXELFORMAT *provided)
2847 /* Some flags must be present in both or neither for a match. */
2848 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2849 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2850 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2852 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2853 return FALSE;
2855 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2856 return FALSE;
2858 if (requested->dwFlags & DDPF_FOURCC)
2859 if (requested->dwFourCC != provided->dwFourCC)
2860 return FALSE;
2862 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2863 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2864 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2865 return FALSE;
2867 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2868 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2869 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2870 return FALSE;
2872 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2873 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2874 return FALSE;
2876 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2877 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2878 |DDPF_BUMPDUDV))
2879 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2880 return FALSE;
2882 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2883 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2884 return FALSE;
2886 return TRUE;
2889 static BOOL
2890 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2891 const DDSURFACEDESC2* provided)
2893 struct compare_info
2895 DWORD flag;
2896 ptrdiff_t offset;
2897 size_t size;
2900 #define CMP(FLAG, FIELD) \
2901 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2902 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2904 static const struct compare_info compare[] =
2906 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2907 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2908 CMP(CAPS, ddsCaps),
2909 CMP(CKDESTBLT, ddckCKDestBlt),
2910 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2911 CMP(CKSRCBLT, ddckCKSrcBlt),
2912 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2913 CMP(HEIGHT, dwHeight),
2914 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2915 CMP(LPSURFACE, lpSurface),
2916 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2917 CMP(PITCH, u1 /* lPitch */),
2918 /* PIXELFORMAT: manual */
2919 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2920 CMP(TEXTURESTAGE, dwTextureStage),
2921 CMP(WIDTH, dwWidth),
2922 /* ZBUFFERBITDEPTH: "obsolete" */
2925 #undef CMP
2927 unsigned int i;
2929 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2930 return FALSE;
2932 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2934 if (requested->dwFlags & compare[i].flag
2935 && memcmp((const char *)provided + compare[i].offset,
2936 (const char *)requested + compare[i].offset,
2937 compare[i].size) != 0)
2938 return FALSE;
2941 if (requested->dwFlags & DDSD_PIXELFORMAT)
2943 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2944 &provided->u4.ddpfPixelFormat))
2945 return FALSE;
2948 return TRUE;
2951 #undef DDENUMSURFACES_SEARCHTYPE
2952 #undef DDENUMSURFACES_MATCHTYPE
2954 /*****************************************************************************
2955 * IDirectDraw7::EnumSurfaces
2957 * Loops through all surfaces attached to this device and calls the
2958 * application callback. This can't be relayed to WineD3DDevice,
2959 * because some WineD3DSurfaces' parents are IParent objects
2961 * Params:
2962 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2963 * DDSD: Description to filter for
2964 * Context: Application-provided pointer, it's passed unmodified to the
2965 * Callback function
2966 * Callback: Address to call for each surface
2968 * Returns:
2969 * DDERR_INVALIDPARAMS if the callback is NULL
2970 * DD_OK on success
2972 *****************************************************************************/
2973 static HRESULT WINAPI
2974 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2975 DWORD Flags,
2976 DDSURFACEDESC2 *DDSD,
2977 void *Context,
2978 LPDDENUMSURFACESCALLBACK7 Callback)
2980 /* The surface enumeration is handled by WineDDraw,
2981 * because it keeps track of all surfaces attached to
2982 * it. The filtering is done by our callback function,
2983 * because WineDDraw doesn't handle ddraw-like surface
2984 * caps structures
2986 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2987 IDirectDrawSurfaceImpl *surf;
2988 BOOL all, nomatch;
2989 DDSURFACEDESC2 desc;
2990 struct list *entry, *entry2;
2992 all = Flags & DDENUMSURFACES_ALL;
2993 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2995 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2996 EnterCriticalSection(&ddraw_cs);
2998 if(!Callback)
3000 LeaveCriticalSection(&ddraw_cs);
3001 return DDERR_INVALIDPARAMS;
3004 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3005 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
3007 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3008 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
3010 desc = surf->surface_desc;
3011 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
3012 if(Callback( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, Context) != DDENUMRET_OK)
3014 LeaveCriticalSection(&ddraw_cs);
3015 return DD_OK;
3019 LeaveCriticalSection(&ddraw_cs);
3020 return DD_OK;
3023 static HRESULT WINAPI
3024 findRenderTarget(IDirectDrawSurface7 *surface,
3025 DDSURFACEDESC2 *desc,
3026 void *ctx)
3028 IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surface);
3029 IDirectDrawSurfaceImpl **target = (IDirectDrawSurfaceImpl **) ctx;
3031 if(!surf->isRenderTarget) {
3032 *target = surf;
3033 IDirectDrawSurface7_Release(surface);
3034 return DDENUMRET_CANCEL;
3037 /* Recurse into the surface tree */
3038 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
3040 IDirectDrawSurface7_Release(surface);
3041 if(*target) return DDENUMRET_CANCEL;
3042 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
3045 /*****************************************************************************
3046 * D3D7CB_CreateRenderTarget
3048 * Callback called by WineD3D to create Surfaces for render target usage
3049 * This function takes the D3D target from the IDirectDrawImpl structure,
3050 * and returns the WineD3DSurface. To avoid double usage, the surface
3051 * is marked as render target afterwards
3053 * Params
3054 * device: The WineD3DDevice's parent
3055 * Width, Height, Format: Dimensions and pixelformat of the render target
3056 * Ignored, because the surface already exists
3057 * MultiSample, MultisampleQuality, Lockable: Ignored for the same reason
3058 * Lockable: ignored
3059 * ppSurface: Address to pass the surface pointer back at
3060 * pSharedHandle: Ignored
3062 * Returns:
3063 * Always returns D3D_OK
3065 *****************************************************************************/
3066 static HRESULT WINAPI
3067 D3D7CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior,
3068 UINT Width, UINT Height,
3069 WINED3DFORMAT Format,
3070 WINED3DMULTISAMPLE_TYPE MultiSample,
3071 DWORD MultisampleQuality,
3072 BOOL Lockable,
3073 IWineD3DSurface** ppSurface,
3074 HANDLE* pSharedHandle)
3076 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3077 IDirectDrawSurfaceImpl *d3dSurface = This->d3d_target, *target = NULL;
3078 TRACE("(%p) call back\n", device);
3080 if(d3dSurface->isRenderTarget)
3082 IDirectDrawSurface7_EnumAttachedSurfaces(ICOM_INTERFACE(d3dSurface, IDirectDrawSurface7),
3083 &target, findRenderTarget);
3085 else
3087 target = d3dSurface;
3090 if(!target)
3092 target = This->d3d_target;
3093 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
3096 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
3098 *ppSurface = target->WineD3DSurface;
3099 target->isRenderTarget = TRUE;
3100 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *ppSurface, d3dSurface);
3101 return D3D_OK;
3104 static HRESULT WINAPI
3105 D3D7CB_CreateDepthStencilSurface(IUnknown *device,
3106 IUnknown *pSuperior,
3107 UINT Width,
3108 UINT Height,
3109 WINED3DFORMAT Format,
3110 WINED3DMULTISAMPLE_TYPE MultiSample,
3111 DWORD MultisampleQuality,
3112 BOOL Discard,
3113 IWineD3DSurface** ppSurface,
3114 HANDLE* pSharedHandle)
3116 /* Create a Depth Stencil surface to make WineD3D happy */
3117 HRESULT hr = D3D_OK;
3118 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3119 DDSURFACEDESC2 ddsd;
3121 TRACE("(%p) call back\n", device);
3123 *ppSurface = NULL;
3125 /* Create a DirectDraw surface */
3126 memset(&ddsd, 0, sizeof(ddsd));
3127 ddsd.dwSize = sizeof(ddsd);
3128 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3129 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3130 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3131 ddsd.dwHeight = Height;
3132 ddsd.dwWidth = Width;
3133 if(Format != 0)
3135 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, Format);
3137 else
3139 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
3142 This->depthstencil = TRUE;
3143 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *) This,
3144 &ddsd,
3145 (IDirectDrawSurface7 **) &This->DepthStencilBuffer,
3146 NULL);
3147 This->depthstencil = FALSE;
3148 if(FAILED(hr))
3150 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
3151 return hr;
3153 *ppSurface = This->DepthStencilBuffer->WineD3DSurface;
3154 return D3D_OK;
3157 /*****************************************************************************
3158 * D3D7CB_CreateAdditionalSwapChain
3160 * Callback function for WineD3D which creates a new WineD3DSwapchain
3161 * interface. It also creates an IParent interface to store that pointer,
3162 * so the WineD3DSwapchain has a parent and can be released when the D3D
3163 * device is destroyed
3164 *****************************************************************************/
3165 static HRESULT WINAPI
3166 D3D7CB_CreateAdditionalSwapChain(IUnknown *device,
3167 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
3168 IWineD3DSwapChain ** ppSwapChain)
3170 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3171 IParentImpl *object = NULL;
3172 HRESULT res = D3D_OK;
3173 IWineD3DSwapChain *swapchain;
3174 IDirectDrawSurfaceImpl *iterator;
3175 TRACE("(%p) call back\n", device);
3177 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
3178 if (NULL == object)
3180 FIXME("Allocation of memory failed\n");
3181 *ppSwapChain = NULL;
3182 return DDERR_OUTOFVIDEOMEMORY;
3185 ICOM_INIT_INTERFACE(object, IParent, IParent_Vtbl);
3186 object->ref = 1;
3188 res = IWineD3DDevice_CreateSwapChain(This->wineD3DDevice,
3189 pPresentationParameters,
3190 &swapchain,
3191 (IUnknown*) ICOM_INTERFACE(object, IParent),
3192 D3D7CB_CreateRenderTarget,
3193 D3D7CB_CreateDepthStencilSurface,
3194 This->ImplType);
3195 if (res != D3D_OK)
3197 FIXME("(%p) call to IWineD3DDevice_CreateSwapChain failed\n", This);
3198 HeapFree(GetProcessHeap(), 0 , object);
3199 *ppSwapChain = NULL;
3201 else
3203 *ppSwapChain = swapchain;
3204 object->child = (IUnknown *) swapchain;
3205 This->d3d_target->wineD3DSwapChain = swapchain;
3206 iterator = This->d3d_target->complex_array[0];
3207 while(iterator) {
3208 iterator->wineD3DSwapChain = swapchain;
3209 iterator = iterator->complex_array[0];
3213 return res;
3216 static HRESULT IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This,
3217 IDirectDrawSurfaceImpl *primary) {
3218 HRESULT hr;
3219 WINED3DPRESENT_PARAMETERS presentation_parameters;
3220 HWND window;
3222 window = This->dest_window;
3224 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
3226 /* Use the surface description for the device parameters, not the
3227 * Device settings. The app might render to an offscreen surface
3229 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
3230 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
3231 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3232 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3233 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3234 presentation_parameters.MultiSampleQuality = 0;
3235 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
3236 presentation_parameters.hDeviceWindow = window;
3237 presentation_parameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3238 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
3239 presentation_parameters.AutoDepthStencilFormat = 0;
3240 presentation_parameters.Flags = 0;
3241 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3242 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3244 This->d3d_target = primary;
3245 hr = IWineD3DDevice_InitGDI(This->wineD3DDevice,
3246 &presentation_parameters,
3247 D3D7CB_CreateAdditionalSwapChain);
3248 This->d3d_target = NULL;
3250 if (hr != D3D_OK)
3252 FIXME("(%p) call to IWineD3DDevice_InitGDI failed\n", This);
3253 primary->wineD3DSwapChain = NULL;
3255 return hr;
3258 /*****************************************************************************
3259 * IDirectDrawImpl_AttachD3DDevice
3261 * Initializes the D3D capabilities of WineD3D
3263 * Params:
3264 * primary: The primary surface for D3D
3266 * Returns
3267 * DD_OK on success,
3268 * DDERR_* otherwise
3270 *****************************************************************************/
3271 static HRESULT
3272 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
3273 IDirectDrawSurfaceImpl *primary)
3275 HRESULT hr;
3276 HWND window = This->dest_window;
3278 WINED3DPRESENT_PARAMETERS localParameters;
3280 TRACE("(%p)->(%p)\n", This, primary);
3282 /* If there's no window, create a hidden window. WineD3D needs it */
3283 if(window == 0 || window == GetDesktopWindow())
3285 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
3286 WS_DISABLED, 0, 0,
3287 GetSystemMetrics(SM_CXSCREEN),
3288 GetSystemMetrics(SM_CYSCREEN),
3289 NULL, NULL, GetModuleHandleA(0), NULL);
3291 ShowWindow(window, SW_HIDE); /* Just to be sure */
3292 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
3294 else
3296 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
3298 This->d3d_window = window;
3300 /* Store the future Render Target surface */
3301 This->d3d_target = primary;
3303 /* Use the surface description for the device parameters, not the
3304 * Device settings. The app might render to an offscreen surface
3306 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
3307 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
3308 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3309 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3310 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3311 localParameters.MultiSampleQuality = 0;
3312 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
3313 localParameters.hDeviceWindow = window;
3314 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3315 localParameters.EnableAutoDepthStencil = TRUE;
3316 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16;
3317 localParameters.Flags = 0;
3318 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3319 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3321 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
3323 /* Set this NOW, otherwise creating the depth stencil surface will cause a
3324 * recursive loop until ram or emulated video memory is full
3326 This->d3d_initialized = TRUE;
3328 hr = IWineD3DDevice_Init3D(This->wineD3DDevice,
3329 &localParameters,
3330 D3D7CB_CreateAdditionalSwapChain);
3331 if(FAILED(hr))
3333 This->d3d_target = NULL;
3334 This->d3d_initialized = FALSE;
3335 return hr;
3338 This->declArraySize = 2;
3339 This->decls = HeapAlloc(GetProcessHeap(),
3340 HEAP_ZERO_MEMORY,
3341 sizeof(*This->decls) * This->declArraySize);
3342 if(!This->decls)
3344 ERR("Error allocating an array for the converted vertex decls\n");
3345 This->declArraySize = 0;
3346 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice,
3347 D3D7CB_DestroyDepthStencilSurface,
3348 D3D7CB_DestroySwapChain);
3349 return E_OUTOFMEMORY;
3352 /* Create an Index Buffer parent */
3353 TRACE("(%p) Successfully initialized 3D\n", This);
3354 return DD_OK;
3357 /*****************************************************************************
3358 * DirectDrawCreateClipper (DDRAW.@)
3360 * Creates a new IDirectDrawClipper object.
3362 * Params:
3363 * Clipper: Address to write the interface pointer to
3364 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3365 * NULL
3367 * Returns:
3368 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3369 * E_OUTOFMEMORY if allocating the object failed
3371 *****************************************************************************/
3372 HRESULT WINAPI
3373 DirectDrawCreateClipper(DWORD Flags,
3374 LPDIRECTDRAWCLIPPER *Clipper,
3375 IUnknown *UnkOuter)
3377 IDirectDrawClipperImpl* object;
3378 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
3380 EnterCriticalSection(&ddraw_cs);
3381 if (UnkOuter != NULL)
3383 LeaveCriticalSection(&ddraw_cs);
3384 return CLASS_E_NOAGGREGATION;
3387 if (!LoadWineD3D())
3389 LeaveCriticalSection(&ddraw_cs);
3390 return DDERR_NODIRECTDRAWSUPPORT;
3393 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3394 sizeof(IDirectDrawClipperImpl));
3395 if (object == NULL)
3397 LeaveCriticalSection(&ddraw_cs);
3398 return E_OUTOFMEMORY;
3401 ICOM_INIT_INTERFACE(object, IDirectDrawClipper, IDirectDrawClipper_Vtbl);
3402 object->ref = 1;
3403 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
3404 if(!object->wineD3DClipper)
3406 HeapFree(GetProcessHeap(), 0, object);
3407 LeaveCriticalSection(&ddraw_cs);
3408 return E_OUTOFMEMORY;
3411 *Clipper = (IDirectDrawClipper *) object;
3412 LeaveCriticalSection(&ddraw_cs);
3413 return DD_OK;
3416 /*****************************************************************************
3417 * IDirectDraw7::CreateClipper
3419 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3421 *****************************************************************************/
3422 static HRESULT WINAPI
3423 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3424 DWORD Flags,
3425 IDirectDrawClipper **Clipper,
3426 IUnknown *UnkOuter)
3428 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3429 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3430 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3433 /*****************************************************************************
3434 * IDirectDraw7::CreatePalette
3436 * Creates a new IDirectDrawPalette object
3438 * Params:
3439 * Flags: The flags for the new clipper
3440 * ColorTable: Color table to assign to the new clipper
3441 * Palette: Address to write the interface pointer to
3442 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3443 * NULL
3445 * Returns:
3446 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3447 * E_OUTOFMEMORY if allocating the object failed
3449 *****************************************************************************/
3450 static HRESULT WINAPI
3451 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3452 DWORD Flags,
3453 PALETTEENTRY *ColorTable,
3454 IDirectDrawPalette **Palette,
3455 IUnknown *pUnkOuter)
3457 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3458 IDirectDrawPaletteImpl *object;
3459 HRESULT hr = DDERR_GENERIC;
3460 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3462 EnterCriticalSection(&ddraw_cs);
3463 if(pUnkOuter != NULL)
3465 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3466 LeaveCriticalSection(&ddraw_cs);
3467 return CLASS_E_NOAGGREGATION;
3470 /* The refcount test shows that a cooplevel is required for this */
3471 if(!This->cooperative_level)
3473 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3474 LeaveCriticalSection(&ddraw_cs);
3475 return DDERR_NOCOOPERATIVELEVELSET;
3478 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3479 if(!object)
3481 ERR("Out of memory when allocating memory for a palette implementation\n");
3482 LeaveCriticalSection(&ddraw_cs);
3483 return E_OUTOFMEMORY;
3486 ICOM_INIT_INTERFACE(object, IDirectDrawPalette, IDirectDrawPalette_Vtbl);
3487 object->ref = 1;
3488 object->ddraw_owner = This;
3490 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags, ColorTable, &object->wineD3DPalette, (IUnknown *) ICOM_INTERFACE(object, IDirectDrawPalette) );
3491 if(hr != DD_OK)
3493 HeapFree(GetProcessHeap(), 0, object);
3494 LeaveCriticalSection(&ddraw_cs);
3495 return hr;
3498 IDirectDraw7_AddRef(iface);
3499 object->ifaceToRelease = (IUnknown *) iface;
3500 *Palette = ICOM_INTERFACE(object, IDirectDrawPalette);
3501 LeaveCriticalSection(&ddraw_cs);
3502 return DD_OK;
3505 /*****************************************************************************
3506 * IDirectDraw7::DuplicateSurface
3508 * Duplicates a surface. The surface memory points to the same memory as
3509 * the original surface, and it's released when the last surface referencing
3510 * it is released. I guess that's beyond Wine's surface management right now
3511 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3512 * test application to implement this)
3514 * Params:
3515 * Src: Address of the source surface
3516 * Dest: Address to write the new surface pointer to
3518 * Returns:
3519 * See IDirectDraw7::CreateSurface
3521 *****************************************************************************/
3522 static HRESULT WINAPI
3523 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3524 IDirectDrawSurface7 *Src,
3525 IDirectDrawSurface7 **Dest)
3527 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3528 IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Src);
3530 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3532 /* For now, simply create a new, independent surface */
3533 return IDirectDraw7_CreateSurface(iface,
3534 &Surf->surface_desc,
3535 Dest,
3536 NULL);
3539 /*****************************************************************************
3540 * IDirectDraw7 VTable
3541 *****************************************************************************/
3542 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3544 /*** IUnknown ***/
3545 IDirectDrawImpl_QueryInterface,
3546 IDirectDrawImpl_AddRef,
3547 IDirectDrawImpl_Release,
3548 /*** IDirectDraw ***/
3549 IDirectDrawImpl_Compact,
3550 IDirectDrawImpl_CreateClipper,
3551 IDirectDrawImpl_CreatePalette,
3552 IDirectDrawImpl_CreateSurface,
3553 IDirectDrawImpl_DuplicateSurface,
3554 IDirectDrawImpl_EnumDisplayModes,
3555 IDirectDrawImpl_EnumSurfaces,
3556 IDirectDrawImpl_FlipToGDISurface,
3557 IDirectDrawImpl_GetCaps,
3558 IDirectDrawImpl_GetDisplayMode,
3559 IDirectDrawImpl_GetFourCCCodes,
3560 IDirectDrawImpl_GetGDISurface,
3561 IDirectDrawImpl_GetMonitorFrequency,
3562 IDirectDrawImpl_GetScanLine,
3563 IDirectDrawImpl_GetVerticalBlankStatus,
3564 IDirectDrawImpl_Initialize,
3565 IDirectDrawImpl_RestoreDisplayMode,
3566 IDirectDrawImpl_SetCooperativeLevel,
3567 IDirectDrawImpl_SetDisplayMode,
3568 IDirectDrawImpl_WaitForVerticalBlank,
3569 /*** IDirectDraw2 ***/
3570 IDirectDrawImpl_GetAvailableVidMem,
3571 /*** IDirectDraw3 ***/
3572 IDirectDrawImpl_GetSurfaceFromDC,
3573 /*** IDirectDraw4 ***/
3574 IDirectDrawImpl_RestoreAllSurfaces,
3575 IDirectDrawImpl_TestCooperativeLevel,
3576 IDirectDrawImpl_GetDeviceIdentifier,
3577 /*** IDirectDraw7 ***/
3578 IDirectDrawImpl_StartModeTest,
3579 IDirectDrawImpl_EvaluateMode
3582 /*****************************************************************************
3583 * IDirectDrawImpl_FindDecl
3585 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3586 * if none was found.
3588 * This function is in ddraw.c and the DDraw object space because D3D7
3589 * vertex buffers are created using the IDirect3D interface to the ddraw
3590 * object, so they can be valid across D3D devices(theoretically. The ddraw
3591 * object also owns the wined3d device
3593 * Parameters:
3594 * This: Device
3595 * fvf: Fvf to find the decl for
3597 * Returns:
3598 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3599 * fvf otherwise.
3601 *****************************************************************************/
3602 IWineD3DVertexDeclaration *
3603 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3604 DWORD fvf)
3606 HRESULT hr;
3607 IWineD3DVertexDeclaration* pDecl = NULL;
3608 int p, low, high; /* deliberately signed */
3609 struct FvfToDecl *convertedDecls = This->decls;
3611 TRACE("Searching for declaration for fvf %08x... ", fvf);
3613 low = 0;
3614 high = This->numConvertedDecls - 1;
3615 while(low <= high) {
3616 p = (low + high) >> 1;
3617 TRACE("%d ", p);
3618 if(convertedDecls[p].fvf == fvf) {
3619 TRACE("found %p\n", convertedDecls[p].decl);
3620 return convertedDecls[p].decl;
3621 } else if(convertedDecls[p].fvf < fvf) {
3622 low = p + 1;
3623 } else {
3624 high = p - 1;
3627 TRACE("not found. Creating and inserting at position %d.\n", low);
3629 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice,
3630 &pDecl,
3631 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7),
3632 fvf);
3633 if (hr != S_OK) return NULL;
3635 if(This->declArraySize == This->numConvertedDecls) {
3636 int grow = max(This->declArraySize / 2, 8);
3637 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3638 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3639 if(!convertedDecls) {
3640 /* This will destroy it */
3641 IWineD3DVertexDeclaration_Release(pDecl);
3642 return NULL;
3644 This->decls = convertedDecls;
3645 This->declArraySize += grow;
3648 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3649 convertedDecls[low].decl = pDecl;
3650 convertedDecls[low].fvf = fvf;
3651 This->numConvertedDecls++;
3653 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
3654 return pDecl;