push b0b97fcd59eff07f047585692fa36859b459324f
[wine/hacks.git] / dlls / ddraw / ddraw.c
blob436846cb4df85ebe15aa95a7e4207f10007b9266
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 WINAPI IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
50 static HRESULT WINAPI IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD, IDirectDrawSurfaceImpl **ppSurf, UINT level);
51 static HRESULT WINAPI 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 TRACE("(%p)->(%p, %p)\n", This, NumCodes, Codes);
892 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
893 0 /* swapchain 0 */,
894 &d3ddm);
896 outsize = NumCodes && Codes ? *NumCodes : 0;
898 for(i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
899 hr = IWineD3D_CheckDeviceFormat(This->wineD3D,
900 WINED3DADAPTER_DEFAULT,
901 WINED3DDEVTYPE_HAL,
902 d3ddm.Format /* AdapterFormat */,
903 0 /* usage */,
904 WINED3DRTYPE_SURFACE,
905 formats[i]);
906 if(SUCCEEDED(hr)) {
907 if(count < outsize) {
908 Codes[count] = formats[i];
910 count++;
913 if(NumCodes) {
914 TRACE("Returning %u FourCC codes\n", count);
915 *NumCodes = count;
918 return DD_OK;
921 /*****************************************************************************
922 * IDirectDraw7::GetMonitorFrequency
924 * Returns the monitor's frequency
926 * Exists in Version 1, 2, 4 and 7
928 * Params:
929 * Freq: Pointer to a DWORD to write the frequency to
931 * Returns
932 * Always returns DD_OK
934 *****************************************************************************/
935 static HRESULT WINAPI
936 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
937 DWORD *Freq)
939 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
940 TRACE("(%p)->(%p)\n", This, Freq);
942 /* Ideally this should be in WineD3D, as it concerns the screen setup,
943 * but for now this should make the games happy
945 *Freq = 60;
946 return DD_OK;
949 /*****************************************************************************
950 * IDirectDraw7::GetVerticalBlankStatus
952 * Returns the Vertical blank status of the monitor. This should be in WineD3D
953 * too basically, but as it's a semi stub, I didn't create a function there
955 * Params:
956 * status: Pointer to a BOOL to be filled with the vertical blank status
958 * Returns
959 * DD_OK on success
960 * DDERR_INVALIDPARAMS if status is NULL
962 *****************************************************************************/
963 static HRESULT WINAPI
964 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
965 BOOL *status)
967 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
968 TRACE("(%p)->(%p)\n", This, status);
970 /* This looks sane, the MSDN suggests it too */
971 EnterCriticalSection(&ddraw_cs);
972 if(!status)
974 LeaveCriticalSection(&ddraw_cs);
975 return DDERR_INVALIDPARAMS;
978 *status = This->fake_vblank;
979 This->fake_vblank = !This->fake_vblank;
980 LeaveCriticalSection(&ddraw_cs);
981 return DD_OK;
984 /*****************************************************************************
985 * IDirectDraw7::GetAvailableVidMem
987 * Returns the total and free video memory
989 * Params:
990 * Caps: Specifies the memory type asked for
991 * total: Pointer to a DWORD to be filled with the total memory
992 * free: Pointer to a DWORD to be filled with the free memory
994 * Returns
995 * DD_OK on success
996 * DDERR_INVALIDPARAMS of free and total are NULL
998 *****************************************************************************/
999 static HRESULT WINAPI
1000 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
1002 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1003 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
1005 if(TRACE_ON(ddraw))
1007 TRACE("(%p) Asked for memory with description: ", This);
1008 DDRAW_dump_DDSCAPS2(Caps);
1010 EnterCriticalSection(&ddraw_cs);
1012 /* Todo: System memory vs local video memory vs non-local video memory
1013 * The MSDN also mentions differences between texture memory and other
1014 * resources, but that's not important
1017 if( (!total) && (!free) )
1019 LeaveCriticalSection(&ddraw_cs);
1020 return DDERR_INVALIDPARAMS;
1023 if(total) *total = This->total_vidmem;
1024 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
1026 LeaveCriticalSection(&ddraw_cs);
1027 return DD_OK;
1030 /*****************************************************************************
1031 * IDirectDraw7::Initialize
1033 * Initializes a DirectDraw interface.
1035 * Params:
1036 * GUID: Interface identifier. Well, don't know what this is really good
1037 * for
1039 * Returns
1040 * Returns DD_OK on the first call,
1041 * DDERR_ALREADYINITIALIZED on repeated calls
1043 *****************************************************************************/
1044 static HRESULT WINAPI
1045 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
1046 GUID *Guid)
1048 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1049 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
1051 if(This->initialized)
1053 return DDERR_ALREADYINITIALIZED;
1055 else
1057 return DD_OK;
1061 /*****************************************************************************
1062 * IDirectDraw7::FlipToGDISurface
1064 * "Makes the surface that the GDI writes to the primary surface"
1065 * Looks like some windows specific thing we don't have to care about.
1066 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1067 * show error boxes ;)
1068 * Well, just return DD_OK.
1070 * Returns:
1071 * Always returns DD_OK
1073 *****************************************************************************/
1074 static HRESULT WINAPI
1075 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1077 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1078 TRACE("(%p)\n", This);
1080 return DD_OK;
1083 /*****************************************************************************
1084 * IDirectDraw7::WaitForVerticalBlank
1086 * This method allows applications to get in sync with the vertical blank
1087 * interval.
1088 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1089 * redraw the screen, most likely because of this stub
1091 * Parameters:
1092 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1093 * or DDWAITVB_BLOCKEND
1094 * h: Not used, according to MSDN
1096 * Returns:
1097 * Always returns DD_OK
1099 *****************************************************************************/
1100 static HRESULT WINAPI
1101 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1102 DWORD Flags,
1103 HANDLE h)
1105 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1106 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1108 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1109 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1110 return DDERR_UNSUPPORTED; /* unchecked */
1112 return DD_OK;
1115 /*****************************************************************************
1116 * IDirectDraw7::GetScanLine
1118 * Returns the scan line that is being drawn on the monitor
1120 * Parameters:
1121 * Scanline: Address to write the scan line value to
1123 * Returns:
1124 * Always returns DD_OK
1126 *****************************************************************************/
1127 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1129 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1130 static BOOL hide = FALSE;
1131 WINED3DDISPLAYMODE Mode;
1133 /* This function is called often, so print the fixme only once */
1134 EnterCriticalSection(&ddraw_cs);
1135 if(!hide)
1137 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1138 hide = TRUE;
1141 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1143 &Mode);
1145 /* Fake the line sweeping of the monitor */
1146 /* FIXME: We should synchronize with a source to keep the refresh rate */
1147 *Scanline = This->cur_scanline++;
1148 /* Assume 20 scan lines in the vertical blank */
1149 if (This->cur_scanline >= Mode.Height + 20)
1150 This->cur_scanline = 0;
1152 LeaveCriticalSection(&ddraw_cs);
1153 return DD_OK;
1156 /*****************************************************************************
1157 * IDirectDraw7::TestCooperativeLevel
1159 * Informs the application about the state of the video adapter, depending
1160 * on the cooperative level
1162 * Returns:
1163 * DD_OK if the device is in a sane state
1164 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1165 * if the state is not correct(See below)
1167 *****************************************************************************/
1168 static HRESULT WINAPI
1169 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1171 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1172 HRESULT hr;
1173 TRACE("(%p)\n", This);
1175 EnterCriticalSection(&ddraw_cs);
1176 /* Description from MSDN:
1177 * For fullscreen apps return DDERR_NOEXCLUSIVEMODE if the user switched
1178 * away from the app with e.g. alt-tab. Windowed apps receive
1179 * DDERR_EXCLUSIVEMODEALREADYSET if another application created a
1180 * DirectDraw object in exclusive mode. DDERR_WRONGMODE is returned,
1181 * when the video mode has changed
1184 hr = IWineD3DDevice_TestCooperativeLevel(This->wineD3DDevice);
1186 /* Fix the result value. These values are mapped from their
1187 * d3d9 counterpart.
1189 switch(hr)
1191 case WINED3DERR_DEVICELOST:
1192 if(This->cooperative_level & DDSCL_EXCLUSIVE)
1194 LeaveCriticalSection(&ddraw_cs);
1195 return DDERR_NOEXCLUSIVEMODE;
1197 else
1199 LeaveCriticalSection(&ddraw_cs);
1200 return DDERR_EXCLUSIVEMODEALREADYSET;
1203 case WINED3DERR_DEVICENOTRESET:
1204 LeaveCriticalSection(&ddraw_cs);
1205 return DD_OK;
1207 case WINED3D_OK:
1208 LeaveCriticalSection(&ddraw_cs);
1209 return DD_OK;
1211 case WINED3DERR_DRIVERINTERNALERROR:
1212 default:
1213 ERR("(%p) Unexpected return value %08x from wineD3D, "
1214 " returning DD_OK\n", This, hr);
1217 LeaveCriticalSection(&ddraw_cs);
1218 return DD_OK;
1221 /*****************************************************************************
1222 * IDirectDraw7::GetGDISurface
1224 * Returns the surface that GDI is treating as the primary surface.
1225 * For Wine this is the front buffer
1227 * Params:
1228 * GDISurface: Address to write the surface pointer to
1230 * Returns:
1231 * DD_OK if the surface was found
1232 * DDERR_NOTFOUND if the GDI surface wasn't found
1234 *****************************************************************************/
1235 static HRESULT WINAPI
1236 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1237 IDirectDrawSurface7 **GDISurface)
1239 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1240 IWineD3DSurface *Surf;
1241 IDirectDrawSurface7 *ddsurf;
1242 HRESULT hr;
1243 DDSCAPS2 ddsCaps;
1244 TRACE("(%p)->(%p)\n", This, GDISurface);
1246 /* Get the back buffer from the wineD3DDevice and search its
1247 * attached surfaces for the front buffer
1249 EnterCriticalSection(&ddraw_cs);
1250 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1251 0, /* SwapChain */
1252 0, /* first back buffer*/
1253 WINED3DBACKBUFFER_TYPE_MONO,
1254 &Surf);
1256 if( (hr != D3D_OK) ||
1257 (!Surf) )
1259 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1260 LeaveCriticalSection(&ddraw_cs);
1261 return DDERR_NOTFOUND;
1264 /* GetBackBuffer AddRef()ed the surface, release it */
1265 IWineD3DSurface_Release(Surf);
1267 IWineD3DSurface_GetParent(Surf,
1268 (IUnknown **) &ddsurf);
1269 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1271 /* Find the front buffer */
1272 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1273 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1274 &ddsCaps,
1275 GDISurface);
1276 if(hr != DD_OK)
1278 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1281 /* The AddRef is OK this time */
1282 LeaveCriticalSection(&ddraw_cs);
1283 return hr;
1286 /*****************************************************************************
1287 * IDirectDraw7::EnumDisplayModes
1289 * Enumerates the supported Display modes. The modes can be filtered with
1290 * the DDSD parameter.
1292 * Params:
1293 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1294 * DDSD: Surface description to filter the modes
1295 * Context: Pointer passed back to the callback function
1296 * cb: Application-provided callback function
1298 * Returns:
1299 * DD_OK on success
1300 * DDERR_INVALIDPARAMS if the callback wasn't set
1302 *****************************************************************************/
1303 static HRESULT WINAPI
1304 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1305 DWORD Flags,
1306 DDSURFACEDESC2 *DDSD,
1307 void *Context,
1308 LPDDENUMMODESCALLBACK2 cb)
1310 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1311 unsigned int modenum, fmt;
1312 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1313 WINED3DDISPLAYMODE mode;
1314 DDSURFACEDESC2 callback_sd;
1315 WINED3DDISPLAYMODE *enum_modes = NULL;
1316 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1318 WINED3DFORMAT checkFormatList[] =
1320 WINED3DFMT_R8G8B8,
1321 WINED3DFMT_A8R8G8B8,
1322 WINED3DFMT_X8R8G8B8,
1323 WINED3DFMT_R5G6B5,
1324 WINED3DFMT_X1R5G5B5,
1325 WINED3DFMT_A1R5G5B5,
1326 WINED3DFMT_A4R4G4B4,
1327 WINED3DFMT_R3G3B2,
1328 WINED3DFMT_A8R3G3B2,
1329 WINED3DFMT_X4R4G4B4,
1330 WINED3DFMT_A2B10G10R10,
1331 WINED3DFMT_A8B8G8R8,
1332 WINED3DFMT_X8B8G8R8,
1333 WINED3DFMT_A2R10G10B10,
1334 WINED3DFMT_A8P8,
1335 WINED3DFMT_P8
1338 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1340 EnterCriticalSection(&ddraw_cs);
1341 /* This looks sane */
1342 if(!cb)
1344 LeaveCriticalSection(&ddraw_cs);
1345 return DDERR_INVALIDPARAMS;
1348 if(DDSD)
1350 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1351 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1354 if(!(Flags & DDEDM_REFRESHRATES))
1356 enum_mode_array_size = 16;
1357 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1358 if (!enum_modes)
1360 ERR("Out of memory\n");
1361 LeaveCriticalSection(&ddraw_cs);
1362 return DDERR_OUTOFMEMORY;
1366 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1368 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1370 continue;
1373 modenum = 0;
1374 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1375 WINED3DADAPTER_DEFAULT,
1376 checkFormatList[fmt],
1377 modenum++,
1378 &mode) == WINED3D_OK)
1380 if(DDSD)
1382 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1383 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1386 if(!(Flags & DDEDM_REFRESHRATES))
1388 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1389 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1390 * to be reduced to a single unique result in such case.
1392 BOOL found = FALSE;
1393 unsigned i;
1395 for (i = 0; i < enum_mode_count; i++)
1397 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1398 enum_modes[i].Format == mode.Format)
1400 found = TRUE;
1401 break;
1405 if(found) continue;
1408 memset(&callback_sd, 0, sizeof(callback_sd));
1409 callback_sd.dwSize = sizeof(callback_sd);
1410 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1412 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1413 if(Flags & DDEDM_REFRESHRATES)
1415 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1416 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1419 callback_sd.dwWidth = mode.Width;
1420 callback_sd.dwHeight = mode.Height;
1422 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1424 /* Calc pitch and DWORD align like MSDN says */
1425 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1426 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1428 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1429 callback_sd.u2.dwRefreshRate);
1431 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1433 TRACE("Application asked to terminate the enumeration\n");
1434 HeapFree(GetProcessHeap(), 0, enum_modes);
1435 LeaveCriticalSection(&ddraw_cs);
1436 return DD_OK;
1439 if(!(Flags & DDEDM_REFRESHRATES))
1441 if (enum_mode_count == enum_mode_array_size)
1443 WINED3DDISPLAYMODE *new_enum_modes;
1445 enum_mode_array_size *= 2;
1446 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1448 if (!new_enum_modes)
1450 ERR("Out of memory\n");
1451 HeapFree(GetProcessHeap(), 0, enum_modes);
1452 LeaveCriticalSection(&ddraw_cs);
1453 return DDERR_OUTOFMEMORY;
1456 enum_modes = new_enum_modes;
1459 enum_modes[enum_mode_count++] = mode;
1464 TRACE("End of enumeration\n");
1465 HeapFree(GetProcessHeap(), 0, enum_modes);
1466 LeaveCriticalSection(&ddraw_cs);
1467 return DD_OK;
1470 /*****************************************************************************
1471 * IDirectDraw7::EvaluateMode
1473 * Used with IDirectDraw7::StartModeTest to test video modes.
1474 * EvaluateMode is used to pass or fail a mode, and continue with the next
1475 * mode
1477 * Params:
1478 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1479 * Timeout: Returns the amount of seconds left before the mode would have
1480 * been failed automatically
1482 * Returns:
1483 * This implementation always DD_OK, because it's a stub
1485 *****************************************************************************/
1486 static HRESULT WINAPI
1487 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1488 DWORD Flags,
1489 DWORD *Timeout)
1491 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1492 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1494 /* When implementing this, implement it in WineD3D */
1496 return DD_OK;
1499 /*****************************************************************************
1500 * IDirectDraw7::GetDeviceIdentifier
1502 * Returns the device identifier, which gives information about the driver
1503 * Our device identifier is defined at the beginning of this file.
1505 * Params:
1506 * DDDI: Address for the returned structure
1507 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1509 * Returns:
1510 * On success it returns DD_OK
1511 * DDERR_INVALIDPARAMS if DDDI is NULL
1513 *****************************************************************************/
1514 static HRESULT WINAPI
1515 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1516 DDDEVICEIDENTIFIER2 *DDDI,
1517 DWORD Flags)
1519 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1520 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1522 if(!DDDI)
1523 return DDERR_INVALIDPARAMS;
1525 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1526 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1527 * to any modern hardware, nor is it interesting for Wine, so ignore it
1530 *DDDI = deviceidentifier;
1531 return DD_OK;
1534 /*****************************************************************************
1535 * IDirectDraw7::GetSurfaceFromDC
1537 * Returns the Surface for a GDI device context handle.
1538 * Is this related to IDirectDrawSurface::GetDC ???
1540 * Params:
1541 * hdc: hdc to return the surface for
1542 * Surface: Address to write the surface pointer to
1544 * Returns:
1545 * Always returns DD_OK because it's a stub
1547 *****************************************************************************/
1548 static HRESULT WINAPI
1549 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1550 HDC hdc,
1551 IDirectDrawSurface7 **Surface)
1553 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1554 FIXME("(%p)->(%p,%p): Stub!\n", This, hdc, Surface);
1556 /* Implementation idea if needed: Loop through all surfaces and compare
1557 * their hdc with hdc. Implement it in WineD3D! */
1558 return DDERR_NOTFOUND;
1561 /*****************************************************************************
1562 * IDirectDraw7::RestoreAllSurfaces
1564 * Calls the restore method of all surfaces
1566 * Params:
1568 * Returns:
1569 * Always returns DD_OK because it's a stub
1571 *****************************************************************************/
1572 static HRESULT WINAPI
1573 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1575 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1576 FIXME("(%p): Stub\n", This);
1578 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1579 * get their parent and call their restore method. Do not implement
1580 * it in WineD3D, as restoring a surface means re-creating the
1581 * WineD3DDSurface
1583 return DD_OK;
1586 /*****************************************************************************
1587 * IDirectDraw7::StartModeTest
1589 * Tests the specified video modes to update the system registry with
1590 * refresh rate information. StartModeTest starts the mode test,
1591 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1592 * isn't called within 15 seconds, the mode is failed automatically
1594 * As refresh rates are handled by the X server, I don't think this
1595 * Method is important
1597 * Params:
1598 * Modes: An array of mode specifications
1599 * NumModes: The number of modes in Modes
1600 * Flags: Some flags...
1602 * Returns:
1603 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1604 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1605 * otherwise DD_OK
1607 *****************************************************************************/
1608 static HRESULT WINAPI
1609 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1610 SIZE *Modes,
1611 DWORD NumModes,
1612 DWORD Flags)
1614 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1615 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1617 /* This looks sane */
1618 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1620 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1621 * As it is not, DDERR_TESTFINISHED is returned
1622 * (hopefully that's correct
1624 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1625 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1628 return DD_OK;
1631 /*****************************************************************************
1632 * IDirectDrawImpl_RecreateSurfacesCallback
1634 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1635 * It re-recreates the WineD3DSurface. It's pretty straightforward
1637 *****************************************************************************/
1638 HRESULT WINAPI
1639 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1640 DDSURFACEDESC2 *desc,
1641 void *Context)
1643 IDirectDrawSurfaceImpl *surfImpl = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1644 IDirectDrawSurface7,
1645 surf);
1646 IDirectDrawImpl *This = surfImpl->ddraw;
1647 IUnknown *Parent;
1648 IParentImpl *parImpl = NULL;
1649 IWineD3DSurface *wineD3DSurface;
1650 IWineD3DSwapChain *swapchain;
1651 HRESULT hr;
1652 void *tmp;
1653 IWineD3DClipper *clipper = NULL;
1655 WINED3DSURFACE_DESC Desc;
1656 WINED3DFORMAT Format;
1657 WINED3DRESOURCETYPE Type;
1658 DWORD Usage;
1659 WINED3DPOOL Pool;
1660 UINT Size;
1662 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1663 DWORD MultiSampleQuality;
1664 UINT Width;
1665 UINT Height;
1667 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1669 /* For the enumeration */
1670 IDirectDrawSurface7_Release(surf);
1672 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1674 /* Get the objects */
1675 swapchain = surfImpl->wineD3DSwapChain;
1676 surfImpl->wineD3DSwapChain = NULL;
1677 wineD3DSurface = surfImpl->WineD3DSurface;
1678 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1679 IUnknown_Release(Parent); /* For the getParent */
1681 /* Is the parent an IParent interface? */
1682 if(IUnknown_QueryInterface(Parent, &IID_IParent, &tmp) == S_OK)
1684 /* It is a IParent interface! */
1685 IUnknown_Release(Parent); /* For the QueryInterface */
1686 parImpl = ICOM_OBJECT(IParentImpl, IParent, Parent);
1687 /* Release the reference the parent interface is holding */
1688 IWineD3DSurface_Release(wineD3DSurface);
1691 /* get the clipper */
1692 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1694 /* Get the surface properties */
1695 Desc.Format = &Format;
1696 Desc.Type = &Type;
1697 Desc.Usage = &Usage;
1698 Desc.Pool = &Pool;
1699 Desc.Size = &Size;
1700 Desc.MultiSampleType = &MultiSampleType;
1701 Desc.MultiSampleQuality = &MultiSampleQuality;
1702 Desc.Width = &Width;
1703 Desc.Height = &Height;
1705 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1706 if(hr != D3D_OK) return hr;
1708 if(swapchain) {
1709 /* If there's a swapchain, it owns the IParent interface. Create a new one for the
1710 * new surface
1712 parImpl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*parImpl));
1713 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
1714 parImpl->ref = 1;
1716 Parent = (IUnknown *) parImpl;
1719 /* Create the new surface */
1720 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
1721 Width, Height, Format,
1722 TRUE /* Lockable */,
1723 FALSE /* Discard */,
1724 surfImpl->mipmap_level,
1725 &surfImpl->WineD3DSurface,
1726 Type,
1727 Usage,
1728 Pool,
1729 MultiSampleType,
1730 MultiSampleQuality,
1731 0 /* SharedHandle */,
1732 This->ImplType,
1733 Parent);
1735 if(hr != D3D_OK)
1736 return hr;
1738 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1740 /* Update the IParent if it exists */
1741 if(parImpl)
1743 parImpl->child = (IUnknown *) surfImpl->WineD3DSurface;
1744 /* Add a reference for the IParent */
1745 IWineD3DSurface_AddRef(surfImpl->WineD3DSurface);
1747 /* TODO: Copy the surface content, except for render targets */
1749 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
1750 * the swapchain
1752 if(swapchain) {
1753 /* The backbuffers have the swapchain set as well, but the primary
1754 * owns it and destroys it
1756 if(surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
1757 IWineD3DDevice_UninitGDI(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1759 surfImpl->isRenderTarget = FALSE;
1760 } else {
1761 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1762 TRACE("Surface released successful, next surface\n");
1763 else
1764 ERR("Something's still holding the old WineD3DSurface\n");
1767 surfImpl->ImplType = This->ImplType;
1769 if(clipper)
1771 IWineD3DClipper_Release(clipper);
1773 return DDENUMRET_OK;
1776 /*****************************************************************************
1777 * IDirectDrawImpl_RecreateAllSurfaces
1779 * A function, that converts all wineD3DSurfaces to the new implementation type
1780 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1781 * new WineD3DSurface, copies the content and releases the old surface
1783 *****************************************************************************/
1784 static HRESULT
1785 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1787 DDSURFACEDESC2 desc;
1788 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1790 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1792 /* Should happen almost never */
1793 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1794 /* Shutdown d3d */
1795 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroyDepthStencilSurface, D3D7CB_DestroySwapChain);
1797 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1799 memset(&desc, 0, sizeof(desc));
1800 desc.dwSize = sizeof(desc);
1802 return IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(This, IDirectDraw7),
1804 &desc,
1805 This,
1806 IDirectDrawImpl_RecreateSurfacesCallback);
1809 /*****************************************************************************
1810 * D3D7CB_CreateSurface
1812 * Callback function for IDirect3DDevice_CreateTexture. It searches for the
1813 * correct mipmap sublevel, and returns it to WineD3D.
1814 * The surfaces are created already by IDirectDraw7::CreateSurface
1816 * Params:
1817 * With, Height: With and height of the surface
1818 * Format: The requested format
1819 * Usage, Pool: D3DUSAGE and D3DPOOL of the surface
1820 * level: The mipmap level
1821 * Face: The cube map face type
1822 * Surface: Pointer to pass the created surface back at
1823 * SharedHandle: NULL
1825 * Returns:
1826 * D3D_OK
1828 *****************************************************************************/
1829 static HRESULT WINAPI
1830 D3D7CB_CreateSurface(IUnknown *device,
1831 IUnknown *pSuperior,
1832 UINT Width, UINT Height,
1833 WINED3DFORMAT Format,
1834 DWORD Usage, WINED3DPOOL Pool, UINT level,
1835 WINED3DCUBEMAP_FACES Face,
1836 IWineD3DSurface **Surface,
1837 HANDLE *SharedHandle)
1839 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
1840 IDirectDrawSurfaceImpl *surf = NULL;
1841 int i = 0;
1842 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
1843 TRACE("(%p) call back. surf=%p. Face %d level %d\n", device, This->tex_root, Face, level);
1845 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
1846 switch(Face)
1848 case WINED3DCUBEMAP_FACE_POSITIVE_X:
1849 TRACE("Asked for positive x\n");
1850 if(searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP) {
1851 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
1853 surf = This->tex_root; break;
1854 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
1855 TRACE("Asked for negative x\n");
1856 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
1857 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
1858 TRACE("Asked for positive y\n");
1859 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
1860 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
1861 TRACE("Asked for negative y\n");
1862 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
1863 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
1864 TRACE("Asked for positive z\n");
1865 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
1866 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
1867 TRACE("Asked for negative z\n");
1868 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
1869 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
1872 if(!surf)
1874 IDirectDrawSurface7 *attached;
1875 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This->tex_root, IDirectDrawSurface7),
1876 &searchcaps,
1877 &attached);
1878 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1879 IDirectDrawSurface7_Release(attached);
1881 if(!surf) ERR("root search surface not found\n");
1883 /* Find the wanted mipmap. There are enough mipmaps in the chain */
1884 while(i < level)
1886 IDirectDrawSurface7 *attached;
1887 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(surf, IDirectDrawSurface7),
1888 &searchcaps,
1889 &attached);
1890 if(!attached) ERR("Surface not found\n");
1891 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1892 IDirectDrawSurface7_Release(attached);
1893 i++;
1896 /* Return the surface */
1897 *Surface = surf->WineD3DSurface;
1899 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *Surface, surf);
1900 return D3D_OK;
1903 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1904 IUnknown* swapChainParent;
1905 TRACE("(%p) call back\n", pSwapChain);
1907 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1908 IUnknown_Release(swapChainParent);
1909 return IUnknown_Release(swapChainParent);
1912 ULONG WINAPI D3D7CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
1913 IUnknown* surfaceParent;
1914 TRACE("(%p) call back\n", pSurface);
1916 IWineD3DSurface_GetParent(pSurface, &surfaceParent);
1917 IUnknown_Release(surfaceParent);
1918 return IUnknown_Release(surfaceParent);
1921 /*****************************************************************************
1922 * IDirectDrawImpl_CreateNewSurface
1924 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1925 * with the passed parameters.
1927 * Params:
1928 * DDSD: Description of the surface to create
1929 * Surf: Address to store the interface pointer at
1931 * Returns:
1932 * DD_OK on success
1934 *****************************************************************************/
1935 static HRESULT WINAPI
1936 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1937 DDSURFACEDESC2 *pDDSD,
1938 IDirectDrawSurfaceImpl **ppSurf,
1939 UINT level)
1941 HRESULT hr;
1942 UINT Width = 0, Height = 0;
1943 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1944 WINED3DRESOURCETYPE ResType = WINED3DRTYPE_SURFACE;
1945 DWORD Usage = 0;
1946 WINED3DSURFTYPE ImplType = This->ImplType;
1947 WINED3DSURFACE_DESC Desc;
1948 IUnknown *Parent;
1949 IParentImpl *parImpl = NULL;
1950 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1952 /* Dummies for GetDesc */
1953 WINED3DPOOL dummy_d3dpool;
1954 WINED3DMULTISAMPLE_TYPE dummy_mst;
1955 UINT dummy_uint;
1956 DWORD dummy_dword;
1958 if (TRACE_ON(ddraw))
1960 TRACE(" (%p) Requesting surface desc :\n", This);
1961 DDRAW_dump_surface_desc(pDDSD);
1964 /* Select the surface type, if it wasn't choosen yet */
1965 if(ImplType == SURFACE_UNKNOWN)
1967 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1968 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1970 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1971 ImplType = SURFACE_OPENGL;
1974 /* Otherwise use GDI surfaces for now */
1975 else
1977 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1978 ImplType = SURFACE_GDI;
1981 /* Policy if all surface implementations are available:
1982 * First, check if a default type was set with winecfg. If not,
1983 * try Xrender surfaces, and use them if they work. Next, check if
1984 * accelerated OpenGL is available, and use GL surfaces in this
1985 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
1986 * was created, always use OpenGL surfaces.
1988 * (Note: Xrender surfaces are not implemented for now, the
1989 * unaccelerated implementation uses GDI to render in Software)
1992 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
1993 * be re-created. This could be done with IDirectDrawSurface7::Restore
1995 This->ImplType = ImplType;
1997 else
1999 if((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE ) &&
2000 (This->ImplType != SURFACE_OPENGL ) && DefaultSurfaceType == SURFACE_UNKNOWN)
2002 /* We have to change to OpenGL,
2003 * and re-create all WineD3DSurfaces
2005 ImplType = SURFACE_OPENGL;
2006 This->ImplType = ImplType;
2007 TRACE("(%p) Re-creating all surfaces\n", This);
2008 IDirectDrawImpl_RecreateAllSurfaces(This);
2009 TRACE("(%p) Done recreating all surfaces\n", This);
2011 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2013 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
2014 /* Do not fail surface creation, only fail 3D device creation */
2018 if (!(pDDSD->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)) &&
2019 !((pDDSD->ddsCaps.dwCaps & DDSCAPS_TEXTURE) && (pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)) )
2021 /* Tests show surfaces without memory flags get these flags added right after creation. */
2022 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
2024 /* Get the correct wined3d usage */
2025 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
2026 DDSCAPS_3DDEVICE ) )
2028 Usage |= WINED3DUSAGE_RENDERTARGET;
2030 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
2032 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
2034 Usage |= WINED3DUSAGE_OVERLAY;
2036 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
2038 /* The depth stencil creation callback sets this flag.
2039 * Set the WineD3D usage to let it know that it's a depth
2040 * Stencil surface.
2042 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
2044 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2046 Pool = WINED3DPOOL_SYSTEMMEM;
2048 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
2050 Pool = WINED3DPOOL_MANAGED;
2051 /* Managed textures have the system memory flag set */
2052 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
2054 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
2056 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
2057 * and texturemanage
2059 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
2062 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
2063 if(Format == WINED3DFMT_UNKNOWN)
2065 ERR("Unsupported / Unknown pixelformat\n");
2066 return DDERR_INVALIDPIXELFORMAT;
2069 /* Create the Surface object */
2070 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
2071 if(!*ppSurf)
2073 ERR("(%p) Error allocating memory for a surface\n", This);
2074 return DDERR_OUTOFVIDEOMEMORY;
2076 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface7, IDirectDrawSurface7_Vtbl);
2077 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface3, IDirectDrawSurface3_Vtbl);
2078 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawGammaControl, IDirectDrawGammaControl_Vtbl);
2079 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture2, IDirect3DTexture2_Vtbl);
2080 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture, IDirect3DTexture1_Vtbl);
2081 (*ppSurf)->ref = 1;
2082 (*ppSurf)->version = 7;
2083 (*ppSurf)->ddraw = This;
2084 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
2085 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2086 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
2088 /* Surface attachments */
2089 (*ppSurf)->next_attached = NULL;
2090 (*ppSurf)->first_attached = *ppSurf;
2092 /* Needed to re-create the surface on an implementation change */
2093 (*ppSurf)->ImplType = ImplType;
2095 /* For D3DDevice creation */
2096 (*ppSurf)->isRenderTarget = FALSE;
2098 /* A trace message for debugging */
2099 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
2101 if(pDDSD->ddsCaps.dwCaps & ( DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE) )
2103 /* Render targets and textures need a IParent interface,
2104 * because WineD3D will destroy them when the swapchain
2105 * is released
2107 parImpl = HeapAlloc(GetProcessHeap(), 0, sizeof(IParentImpl));
2108 if(!parImpl)
2110 ERR("Out of memory when allocating memory for a IParent implementation\n");
2111 return DDERR_OUTOFMEMORY;
2113 parImpl->ref = 1;
2114 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
2115 Parent = (IUnknown *) ICOM_INTERFACE(parImpl, IParent);
2116 TRACE("Using IParent interface %p as parent\n", parImpl);
2118 else
2120 /* Use the surface as parent */
2121 Parent = (IUnknown *) ICOM_INTERFACE(*ppSurf, IDirectDrawSurface7);
2122 TRACE("Using Surface interface %p as parent\n", *ppSurf);
2125 /* Now create the WineD3D Surface */
2126 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
2127 pDDSD->dwWidth,
2128 pDDSD->dwHeight,
2129 Format,
2130 TRUE /* Lockable */,
2131 FALSE /* Discard */,
2132 level,
2133 &(*ppSurf)->WineD3DSurface,
2134 ResType, Usage,
2135 Pool,
2136 WINED3DMULTISAMPLE_NONE,
2137 0 /* MultiSampleQuality */,
2138 0 /* SharedHandle */,
2139 ImplType,
2140 Parent);
2142 if(hr != D3D_OK)
2144 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
2145 return hr;
2148 /* Set the child of the parent implementation if it exists */
2149 if(parImpl)
2151 parImpl->child = (IUnknown *) (*ppSurf)->WineD3DSurface;
2152 /* The IParent releases the WineD3DSurface, and
2153 * the ddraw surface does that too. Hold a reference
2155 IWineD3DSurface_AddRef((*ppSurf)->WineD3DSurface);
2158 /* Increase the surface counter, and attach the surface */
2159 InterlockedIncrement(&This->surfaces);
2160 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2162 /* Here we could store all created surfaces in the DirectDrawImpl structure,
2163 * But this could also be delegated to WineDDraw, as it keeps track of all its
2164 * resources. Not implemented for now, as there are more important things ;)
2167 /* Get the pixel format of the WineD3DSurface and store it.
2168 * Don't use the Format choosen above, WineD3D might have
2169 * changed it
2171 Desc.Format = &Format;
2172 Desc.Type = &ResType;
2173 Desc.Usage = &Usage;
2174 Desc.Pool = &dummy_d3dpool;
2175 Desc.Size = &dummy_uint;
2176 Desc.MultiSampleType = &dummy_mst;
2177 Desc.MultiSampleQuality = &dummy_dword;
2178 Desc.Width = &Width;
2179 Desc.Height = &Height;
2181 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
2182 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
2183 if(hr != D3D_OK)
2185 ERR("IWineD3DSurface::GetDesc failed\n");
2186 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
2187 return hr;
2190 if(Format == WINED3DFMT_UNKNOWN)
2192 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
2194 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
2196 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
2197 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
2198 * WineD3DDevice might not be usable for 3D yet, so an extra method was created.
2199 * TODO: Test other fourcc formats
2201 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
2202 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
2204 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
2205 if(Format == WINED3DFMT_DXT1)
2207 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
2209 else
2211 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
2214 else
2216 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
2217 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
2220 /* Application passed a color key? Set it! */
2221 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
2223 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2224 DDCKEY_DESTOVERLAY,
2225 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
2227 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
2229 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2230 DDCKEY_DESTBLT,
2231 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
2233 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
2235 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2236 DDCKEY_SRCOVERLAY,
2237 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2239 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2241 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2242 DDCKEY_SRCBLT,
2243 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2245 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2247 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2248 if(hr != WINED3D_OK)
2250 /* No need for a trace here, wined3d does that for us */
2251 IDirectDrawSurface7_Release(ICOM_INTERFACE((*ppSurf), IDirectDrawSurface7));
2252 return hr;
2256 return DD_OK;
2258 /*****************************************************************************
2259 * CreateAdditionalSurfaces
2261 * Creates a new mipmap chain.
2263 * Params:
2264 * root: Root surface to attach the newly created chain to
2265 * count: number of surfaces to create
2266 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2267 * effects on the caller
2268 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2269 * creates an additional surface without the mipmapping flags
2271 *****************************************************************************/
2272 static HRESULT
2273 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2274 IDirectDrawSurfaceImpl *root,
2275 UINT count,
2276 DDSURFACEDESC2 DDSD,
2277 BOOL CubeFaceRoot)
2279 UINT i, j, level = 0;
2280 HRESULT hr;
2281 IDirectDrawSurfaceImpl *last = root;
2283 for(i = 0; i < count; i++)
2285 IDirectDrawSurfaceImpl *object2 = NULL;
2287 /* increase the mipmap level, but only if a mipmap is created
2288 * In this case, also halve the size
2290 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2292 level++;
2293 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2294 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2295 /* Set the mipmap sublevel flag according to msdn */
2296 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2298 else
2300 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2302 CubeFaceRoot = FALSE;
2304 hr = IDirectDrawImpl_CreateNewSurface(This,
2305 &DDSD,
2306 &object2,
2307 level);
2308 if(hr != DD_OK)
2310 return hr;
2313 /* Add the new surface to the complex attachment array */
2314 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2316 if(last->complex_array[j]) continue;
2317 last->complex_array[j] = object2;
2318 break;
2320 last = object2;
2322 /* Remove the (possible) back buffer cap from the new surface description,
2323 * because only one surface in the flipping chain is a back buffer, one
2324 * is a front buffer, the others are just primary surfaces.
2326 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2328 return DD_OK;
2331 /*****************************************************************************
2332 * IDirectDraw7::CreateSurface
2334 * Creates a new IDirectDrawSurface object and returns its interface.
2336 * The surface connections with wined3d are a bit tricky. Basically it works
2337 * like this:
2339 * |------------------------| |-----------------|
2340 * | DDraw surface | | WineD3DSurface |
2341 * | | | |
2342 * | WineD3DSurface |-------------->| |
2343 * | Child |<------------->| Parent |
2344 * |------------------------| |-----------------|
2346 * The DDraw surface is the parent of the wined3d surface, and it releases
2347 * the WineD3DSurface when the ddraw surface is destroyed.
2349 * However, for all surfaces which can be in a container in WineD3D,
2350 * we have to do this. These surfaces are usually complex surfaces,
2351 * so this concerns primary surfaces with a front and a back buffer,
2352 * and textures.
2354 * |------------------------| |-----------------|
2355 * | DDraw surface | | Container |
2356 * | | | |
2357 * | Child |<------------->| Parent |
2358 * | Texture |<------------->| |
2359 * | WineD3DSurface |<----| | Levels |<--|
2360 * | Complex connection | | | | |
2361 * |------------------------| | |-----------------| |
2362 * ^ | |
2363 * | | |
2364 * | | |
2365 * | |------------------| | |-----------------| |
2366 * | | IParent | |-------->| WineD3DSurface | |
2367 * | | | | | |
2368 * | | Child |<------------->| Parent | |
2369 * | | | | Container |<--|
2370 * | |------------------| |-----------------| |
2371 * | |
2372 * | |----------------------| |
2373 * | | DDraw surface 2 | |
2374 * | | | |
2375 * |<->| Complex root Child | |
2376 * | | Texture | |
2377 * | | WineD3DSurface |<----| |
2378 * | |----------------------| | |
2379 * | | |
2380 * | |---------------------| | |-----------------| |
2381 * | | IParent | |----->| WineD3DSurface | |
2382 * | | | | | |
2383 * | | Child |<---------->| Parent | |
2384 * | |---------------------| | Container |<--|
2385 * | |-----------------| |
2386 * | |
2387 * | ---More surfaces can follow--- |
2389 * The reason is that the IWineD3DSwapchain(render target container)
2390 * and the IWineD3DTexure(Texture container) release the parents
2391 * of their surface's children, but by releasing the complex root
2392 * the surfaces which are complexly attached to it are destroyed
2393 * too. See IDirectDrawSurface::Release for a more detailed
2394 * explanation.
2396 * Params:
2397 * DDSD: Description of the surface to create
2398 * Surf: Address to store the interface pointer at
2399 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2400 * aggregation, so it has to be NULL
2402 * Returns:
2403 * DD_OK on success
2404 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2405 * DDERR_* if an error occurs
2407 *****************************************************************************/
2408 static HRESULT WINAPI
2409 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2410 DDSURFACEDESC2 *DDSD,
2411 IDirectDrawSurface7 **Surf,
2412 IUnknown *UnkOuter)
2414 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2415 IDirectDrawSurfaceImpl *object = NULL;
2416 HRESULT hr;
2417 LONG extra_surfaces = 0;
2418 DDSURFACEDESC2 desc2;
2419 WINED3DDISPLAYMODE Mode;
2421 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2423 /* Some checks before we start */
2424 if (TRACE_ON(ddraw))
2426 TRACE(" (%p) Requesting surface desc :\n", This);
2427 DDRAW_dump_surface_desc(DDSD);
2429 EnterCriticalSection(&ddraw_cs);
2431 if (UnkOuter != NULL)
2433 FIXME("(%p) : outer != NULL?\n", This);
2434 LeaveCriticalSection(&ddraw_cs);
2435 return CLASS_E_NOAGGREGATION; /* unchecked */
2438 if (Surf == NULL)
2440 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2441 LeaveCriticalSection(&ddraw_cs);
2442 return E_POINTER; /* unchecked */
2445 if (!(DDSD->dwFlags & DDSD_CAPS))
2447 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2448 DDSD->dwFlags |= DDSD_CAPS;
2451 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2453 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2454 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2457 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2459 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2460 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2461 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2464 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2465 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2467 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2468 *Surf = NULL;
2469 LeaveCriticalSection(&ddraw_cs);
2470 return DDERR_NOEXCLUSIVEMODE;
2473 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2474 WARN("Application tried to create an explicit front or back buffer\n");
2475 LeaveCriticalSection(&ddraw_cs);
2476 return DDERR_INVALIDCAPS;
2478 /* Check cube maps but only if the size includes them */
2479 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2481 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2482 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2484 WARN("Cube map faces requested without cube map flag\n");
2485 LeaveCriticalSection(&ddraw_cs);
2486 return DDERR_INVALIDCAPS;
2488 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2489 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2491 WARN("Cube map without faces requested\n");
2492 LeaveCriticalSection(&ddraw_cs);
2493 return DDERR_INVALIDPARAMS;
2496 /* Quick tests confirm those can be created, but we don't do that yet */
2497 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2498 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2500 FIXME("Partial cube maps not supported yet\n");
2504 /* According to the msdn this flag is ignored by CreateSurface */
2505 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2506 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2508 /* Modify some flags */
2509 memset(&desc2, 0, sizeof(desc2));
2510 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2511 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2512 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2513 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2515 /* Get the video mode from WineD3D - we will need it */
2516 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2517 0, /* Swapchain 0 */
2518 &Mode);
2519 if(FAILED(hr))
2521 ERR("Failed to read display mode from wined3d\n");
2522 switch(This->orig_bpp)
2524 case 8:
2525 Mode.Format = WINED3DFMT_P8;
2526 break;
2528 case 15:
2529 Mode.Format = WINED3DFMT_X1R5G5B5;
2530 break;
2532 case 16:
2533 Mode.Format = WINED3DFMT_R5G6B5;
2534 break;
2536 case 24:
2537 Mode.Format = WINED3DFMT_R8G8B8;
2538 break;
2540 case 32:
2541 Mode.Format = WINED3DFMT_X8R8G8B8;
2542 break;
2544 Mode.Width = This->orig_width;
2545 Mode.Height = This->orig_height;
2548 /* No pixelformat given? Use the current screen format */
2549 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2551 desc2.dwFlags |= DDSD_PIXELFORMAT;
2552 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2554 /* Wait: It could be a Z buffer */
2555 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2557 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2559 case 15:
2560 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D15S1);
2561 break;
2562 case 16:
2563 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16);
2564 break;
2565 case 24:
2566 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D24X8);
2567 break;
2568 case 32:
2569 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32);
2570 break;
2571 default:
2572 ERR("Unknown Z buffer bit depth\n");
2575 else
2577 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2581 /* No Width or no Height? Use the original screen size
2583 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2584 !(desc2.dwFlags & DDSD_HEIGHT) )
2586 /* Invalid for non-render targets */
2587 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2589 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2590 *Surf = NULL;
2591 LeaveCriticalSection(&ddraw_cs);
2592 return DDERR_INVALIDPARAMS;
2595 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2596 desc2.dwWidth = Mode.Width;
2597 desc2.dwHeight = Mode.Height;
2600 /* Mipmap count fixes */
2601 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2603 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2605 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2607 /* Mipmap count is given, should not be 0 */
2608 if( desc2.u2.dwMipMapCount == 0 )
2610 LeaveCriticalSection(&ddraw_cs);
2611 return DDERR_INVALIDPARAMS;
2614 else
2616 /* Undocumented feature: Create sublevels until
2617 * either the width or the height is 1
2619 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2620 desc2.dwWidth : desc2.dwHeight;
2621 desc2.u2.dwMipMapCount = 0;
2622 while( min )
2624 desc2.u2.dwMipMapCount += 1;
2625 min >>= 1;
2629 else
2631 /* Not-complex mipmap -> Mipmapcount = 1 */
2632 desc2.u2.dwMipMapCount = 1;
2634 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2636 /* There's a mipmap count in the created surface in any case */
2637 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2639 /* If no mipmap is given, the texture has only one level */
2641 /* The first surface is a front buffer, the back buffer is created afterwards */
2642 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2644 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2647 /* The root surface in a cube map is positive x */
2648 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2650 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2651 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2654 /* Create the first surface */
2655 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2656 if( hr != DD_OK)
2658 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2659 LeaveCriticalSection(&ddraw_cs);
2660 return hr;
2662 object->is_complex_root = TRUE;
2664 *Surf = ICOM_INTERFACE(object, IDirectDrawSurface7);
2666 /* Create Additional surfaces if necessary
2667 * This applies to Primary surfaces which have a back buffer count
2668 * set, but not to mipmap textures. In case of Mipmap textures,
2669 * wineD3D takes care of the creation of additional surfaces
2671 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2673 extra_surfaces = DDSD->dwBackBufferCount;
2674 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2675 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2678 hr = DD_OK;
2679 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2681 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2682 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2683 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2684 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2685 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2686 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2687 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2688 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2689 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2690 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2691 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2692 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2693 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2694 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2695 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2696 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2697 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2700 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2701 if(hr != DD_OK)
2703 /* This destroys and possibly created surfaces too */
2704 IDirectDrawSurface_Release( ICOM_INTERFACE(object, IDirectDrawSurface7) );
2705 LeaveCriticalSection(&ddraw_cs);
2706 return hr;
2709 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2710 * But attach the d3ddevice only if the currently created surface was
2711 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2712 * The only case I can think of where this doesn't apply is when a
2713 * 2D app was configured by the user to run with OpenGL and it didn't create
2714 * the render target as first surface. In this case the render target creation
2715 * will cause the 3D init.
2717 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2718 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2720 IDirectDrawSurfaceImpl *target = object, *surface;
2721 struct list *entry;
2723 /* Search for the primary to use as render target */
2724 LIST_FOR_EACH(entry, &This->surface_list)
2726 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2727 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2728 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2730 /* found */
2731 target = surface;
2732 TRACE("Using primary %p as render target\n", target);
2733 break;
2737 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2738 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2739 if(hr != D3D_OK)
2741 IDirectDrawSurfaceImpl *release_surf;
2742 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2743 *Surf = NULL;
2745 /* The before created surface structures are in an incomplete state here.
2746 * WineD3D holds the reference on the IParents, and it released them on the failure
2747 * already. So the regular release method implementation would fail on the attempt
2748 * to destroy either the IParents or the swapchain. So free the surface here.
2749 * The surface structure here is a list, not a tree, because onscreen targets
2750 * cannot be cube textures
2752 while(object)
2754 release_surf = object;
2755 object = object->complex_array[0];
2756 IDirectDrawSurfaceImpl_Destroy(release_surf);
2758 LeaveCriticalSection(&ddraw_cs);
2759 return hr;
2761 } else if(!(This->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
2762 IDirectDrawImpl_CreateGDISwapChain(This, object);
2765 /* Addref the ddraw interface to keep an reference for each surface */
2766 IDirectDraw7_AddRef(iface);
2767 object->ifaceToRelease = (IUnknown *) iface;
2769 /* Create a WineD3DTexture if a texture was requested */
2770 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2772 UINT levels;
2773 WINED3DFORMAT Format;
2774 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2776 This->tex_root = object;
2778 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2780 /* a mipmap is created, create enough levels */
2781 levels = desc2.u2.dwMipMapCount;
2783 else
2785 /* No mipmap is created, create one level */
2786 levels = 1;
2789 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2790 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2792 Pool = WINED3DPOOL_SYSTEMMEM;
2794 /* Should I forward the MANAGED cap to the managed pool ? */
2796 /* Get the format. It's set already by CreateNewSurface */
2797 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2799 /* The surfaces are already created, the callback only
2800 * passes the IWineD3DSurface to WineD3D
2802 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2804 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice,
2805 DDSD->dwWidth, /* Edgelength */
2806 levels,
2807 0, /* usage */
2808 Format,
2809 Pool,
2810 (IWineD3DCubeTexture **) &object->wineD3DTexture,
2811 0, /* SharedHandle */
2812 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2813 D3D7CB_CreateSurface);
2815 else
2817 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice,
2818 DDSD->dwWidth, DDSD->dwHeight,
2819 levels, /* MipMapCount = Levels */
2820 0, /* usage */
2821 Format,
2822 Pool,
2823 (IWineD3DTexture **) &object->wineD3DTexture,
2824 0, /* SharedHandle */
2825 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2826 D3D7CB_CreateSurface );
2828 This->tex_root = NULL;
2831 LeaveCriticalSection(&ddraw_cs);
2832 return hr;
2835 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2836 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2838 static BOOL
2839 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2840 const DDPIXELFORMAT *provided)
2842 /* Some flags must be present in both or neither for a match. */
2843 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2844 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2845 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2847 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2848 return FALSE;
2850 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2851 return FALSE;
2853 if (requested->dwFlags & DDPF_FOURCC)
2854 if (requested->dwFourCC != provided->dwFourCC)
2855 return FALSE;
2857 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2858 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2859 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2860 return FALSE;
2862 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2863 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2864 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2865 return FALSE;
2867 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2868 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2869 return FALSE;
2871 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2872 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2873 |DDPF_BUMPDUDV))
2874 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2875 return FALSE;
2877 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2878 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2879 return FALSE;
2881 return TRUE;
2884 static BOOL
2885 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2886 const DDSURFACEDESC2* provided)
2888 struct compare_info
2890 DWORD flag;
2891 ptrdiff_t offset;
2892 size_t size;
2895 #define CMP(FLAG, FIELD) \
2896 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2897 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2899 static const struct compare_info compare[] =
2901 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2902 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2903 CMP(CAPS, ddsCaps),
2904 CMP(CKDESTBLT, ddckCKDestBlt),
2905 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2906 CMP(CKSRCBLT, ddckCKSrcBlt),
2907 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2908 CMP(HEIGHT, dwHeight),
2909 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2910 CMP(LPSURFACE, lpSurface),
2911 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2912 CMP(PITCH, u1 /* lPitch */),
2913 /* PIXELFORMAT: manual */
2914 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2915 CMP(TEXTURESTAGE, dwTextureStage),
2916 CMP(WIDTH, dwWidth),
2917 /* ZBUFFERBITDEPTH: "obsolete" */
2920 #undef CMP
2922 unsigned int i;
2924 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2925 return FALSE;
2927 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2929 if (requested->dwFlags & compare[i].flag
2930 && memcmp((const char *)provided + compare[i].offset,
2931 (const char *)requested + compare[i].offset,
2932 compare[i].size) != 0)
2933 return FALSE;
2936 if (requested->dwFlags & DDSD_PIXELFORMAT)
2938 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2939 &provided->u4.ddpfPixelFormat))
2940 return FALSE;
2943 return TRUE;
2946 #undef DDENUMSURFACES_SEARCHTYPE
2947 #undef DDENUMSURFACES_MATCHTYPE
2949 /*****************************************************************************
2950 * IDirectDraw7::EnumSurfaces
2952 * Loops through all surfaces attached to this device and calls the
2953 * application callback. This can't be relayed to WineD3DDevice,
2954 * because some WineD3DSurfaces' parents are IParent objects
2956 * Params:
2957 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2958 * DDSD: Description to filter for
2959 * Context: Application-provided pointer, it's passed unmodified to the
2960 * Callback function
2961 * Callback: Address to call for each surface
2963 * Returns:
2964 * DDERR_INVALIDPARAMS if the callback is NULL
2965 * DD_OK on success
2967 *****************************************************************************/
2968 static HRESULT WINAPI
2969 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2970 DWORD Flags,
2971 DDSURFACEDESC2 *DDSD,
2972 void *Context,
2973 LPDDENUMSURFACESCALLBACK7 Callback)
2975 /* The surface enumeration is handled by WineDDraw,
2976 * because it keeps track of all surfaces attached to
2977 * it. The filtering is done by our callback function,
2978 * because WineDDraw doesn't handle ddraw-like surface
2979 * caps structures
2981 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2982 IDirectDrawSurfaceImpl *surf;
2983 BOOL all, nomatch;
2984 DDSURFACEDESC2 desc;
2985 struct list *entry, *entry2;
2987 all = Flags & DDENUMSURFACES_ALL;
2988 nomatch = Flags & DDENUMSURFACES_NOMATCH;
2990 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
2991 EnterCriticalSection(&ddraw_cs);
2993 if(!Callback)
2995 LeaveCriticalSection(&ddraw_cs);
2996 return DDERR_INVALIDPARAMS;
2999 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3000 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
3002 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3003 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
3005 desc = surf->surface_desc;
3006 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
3007 if(Callback( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, Context) != DDENUMRET_OK)
3009 LeaveCriticalSection(&ddraw_cs);
3010 return DD_OK;
3014 LeaveCriticalSection(&ddraw_cs);
3015 return DD_OK;
3018 static HRESULT WINAPI
3019 findRenderTarget(IDirectDrawSurface7 *surface,
3020 DDSURFACEDESC2 *desc,
3021 void *ctx)
3023 IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surface);
3024 IDirectDrawSurfaceImpl **target = (IDirectDrawSurfaceImpl **) ctx;
3026 if(!surf->isRenderTarget) {
3027 *target = surf;
3028 IDirectDrawSurface7_Release(surface);
3029 return DDENUMRET_CANCEL;
3032 /* Recurse into the surface tree */
3033 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
3035 IDirectDrawSurface7_Release(surface);
3036 if(*target) return DDENUMRET_CANCEL;
3037 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
3040 /*****************************************************************************
3041 * D3D7CB_CreateRenderTarget
3043 * Callback called by WineD3D to create Surfaces for render target usage
3044 * This function takes the D3D target from the IDirectDrawImpl structure,
3045 * and returns the WineD3DSurface. To avoid double usage, the surface
3046 * is marked as render target afterwards
3048 * Params
3049 * device: The WineD3DDevice's parent
3050 * Width, Height, Format: Dimensions and pixelformat of the render target
3051 * Ignored, because the surface already exists
3052 * MultiSample, MultisampleQuality, Lockable: Ignored for the same reason
3053 * Lockable: ignored
3054 * ppSurface: Address to pass the surface pointer back at
3055 * pSharedHandle: Ignored
3057 * Returns:
3058 * Always returns D3D_OK
3060 *****************************************************************************/
3061 static HRESULT WINAPI
3062 D3D7CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior,
3063 UINT Width, UINT Height,
3064 WINED3DFORMAT Format,
3065 WINED3DMULTISAMPLE_TYPE MultiSample,
3066 DWORD MultisampleQuality,
3067 BOOL Lockable,
3068 IWineD3DSurface** ppSurface,
3069 HANDLE* pSharedHandle)
3071 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3072 IDirectDrawSurfaceImpl *d3dSurface = This->d3d_target, *target = NULL;
3073 TRACE("(%p) call back\n", device);
3075 if(d3dSurface->isRenderTarget)
3077 IDirectDrawSurface7_EnumAttachedSurfaces(ICOM_INTERFACE(d3dSurface, IDirectDrawSurface7),
3078 &target, findRenderTarget);
3080 else
3082 target = d3dSurface;
3085 if(!target)
3087 target = This->d3d_target;
3088 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
3091 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
3093 *ppSurface = target->WineD3DSurface;
3094 target->isRenderTarget = TRUE;
3095 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *ppSurface, d3dSurface);
3096 return D3D_OK;
3099 static HRESULT WINAPI
3100 D3D7CB_CreateDepthStencilSurface(IUnknown *device,
3101 IUnknown *pSuperior,
3102 UINT Width,
3103 UINT Height,
3104 WINED3DFORMAT Format,
3105 WINED3DMULTISAMPLE_TYPE MultiSample,
3106 DWORD MultisampleQuality,
3107 BOOL Discard,
3108 IWineD3DSurface** ppSurface,
3109 HANDLE* pSharedHandle)
3111 /* Create a Depth Stencil surface to make WineD3D happy */
3112 HRESULT hr = D3D_OK;
3113 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3114 DDSURFACEDESC2 ddsd;
3116 TRACE("(%p) call back\n", device);
3118 *ppSurface = NULL;
3120 /* Create a DirectDraw surface */
3121 memset(&ddsd, 0, sizeof(ddsd));
3122 ddsd.dwSize = sizeof(ddsd);
3123 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3124 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3125 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3126 ddsd.dwHeight = Height;
3127 ddsd.dwWidth = Width;
3128 if(Format != 0)
3130 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, Format);
3132 else
3134 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
3137 This->depthstencil = TRUE;
3138 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *) This,
3139 &ddsd,
3140 (IDirectDrawSurface7 **) &This->DepthStencilBuffer,
3141 NULL);
3142 This->depthstencil = FALSE;
3143 if(FAILED(hr))
3145 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
3146 return hr;
3148 *ppSurface = This->DepthStencilBuffer->WineD3DSurface;
3149 return D3D_OK;
3152 /*****************************************************************************
3153 * D3D7CB_CreateAdditionalSwapChain
3155 * Callback function for WineD3D which creates a new WineD3DSwapchain
3156 * interface. It also creates an IParent interface to store that pointer,
3157 * so the WineD3DSwapchain has a parent and can be released when the D3D
3158 * device is destroyed
3159 *****************************************************************************/
3160 static HRESULT WINAPI
3161 D3D7CB_CreateAdditionalSwapChain(IUnknown *device,
3162 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
3163 IWineD3DSwapChain ** ppSwapChain)
3165 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3166 IParentImpl *object = NULL;
3167 HRESULT res = D3D_OK;
3168 IWineD3DSwapChain *swapchain;
3169 IDirectDrawSurfaceImpl *iterator;
3170 TRACE("(%p) call back\n", device);
3172 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
3173 if (NULL == object)
3175 FIXME("Allocation of memory failed\n");
3176 *ppSwapChain = NULL;
3177 return DDERR_OUTOFVIDEOMEMORY;
3180 ICOM_INIT_INTERFACE(object, IParent, IParent_Vtbl);
3181 object->ref = 1;
3183 res = IWineD3DDevice_CreateAdditionalSwapChain(This->wineD3DDevice,
3184 pPresentationParameters,
3185 &swapchain,
3186 (IUnknown*) ICOM_INTERFACE(object, IParent),
3187 D3D7CB_CreateRenderTarget,
3188 D3D7CB_CreateDepthStencilSurface,
3189 This->ImplType);
3190 if (res != D3D_OK)
3192 FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
3193 HeapFree(GetProcessHeap(), 0 , object);
3194 *ppSwapChain = NULL;
3196 else
3198 *ppSwapChain = swapchain;
3199 object->child = (IUnknown *) swapchain;
3200 This->d3d_target->wineD3DSwapChain = swapchain;
3201 iterator = This->d3d_target->complex_array[0];
3202 while(iterator) {
3203 iterator->wineD3DSwapChain = swapchain;
3204 iterator = iterator->complex_array[0];
3208 return res;
3211 static HRESULT WINAPI IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This,
3212 IDirectDrawSurfaceImpl *primary) {
3213 HRESULT hr;
3214 WINED3DPRESENT_PARAMETERS presentation_parameters;
3215 HWND window;
3217 window = This->dest_window;
3219 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
3221 /* Use the surface description for the device parameters, not the
3222 * Device settings. The app might render to an offscreen surface
3224 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
3225 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
3226 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3227 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3228 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3229 presentation_parameters.MultiSampleQuality = 0;
3230 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
3231 presentation_parameters.hDeviceWindow = window;
3232 presentation_parameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3233 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
3234 presentation_parameters.AutoDepthStencilFormat = 0;
3235 presentation_parameters.Flags = 0;
3236 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3237 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3239 This->d3d_target = primary;
3240 hr = IWineD3DDevice_InitGDI(This->wineD3DDevice,
3241 &presentation_parameters,
3242 D3D7CB_CreateAdditionalSwapChain);
3243 This->d3d_target = NULL;
3245 if (hr != D3D_OK)
3247 FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
3248 primary->wineD3DSwapChain = NULL;
3250 return hr;
3253 /*****************************************************************************
3254 * IDirectDrawImpl_AttachD3DDevice
3256 * Initializes the D3D capabilities of WineD3D
3258 * Params:
3259 * primary: The primary surface for D3D
3261 * Returns
3262 * DD_OK on success,
3263 * DDERR_* otherwise
3265 *****************************************************************************/
3266 static HRESULT WINAPI
3267 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
3268 IDirectDrawSurfaceImpl *primary)
3270 HRESULT hr;
3271 HWND window = This->dest_window;
3273 WINED3DPRESENT_PARAMETERS localParameters;
3275 TRACE("(%p)->(%p)\n", This, primary);
3277 /* If there's no window, create a hidden window. WineD3D needs it */
3278 if(window == 0)
3280 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
3281 WS_DISABLED, 0, 0,
3282 GetSystemMetrics(SM_CXSCREEN),
3283 GetSystemMetrics(SM_CYSCREEN),
3284 NULL, NULL, GetModuleHandleA(0), NULL);
3286 ShowWindow(window, SW_HIDE); /* Just to be sure */
3287 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
3289 else
3291 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
3293 This->d3d_window = window;
3295 /* Store the future Render Target surface */
3296 This->d3d_target = primary;
3298 /* Use the surface description for the device parameters, not the
3299 * Device settings. The app might render to an offscreen surface
3301 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
3302 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
3303 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3304 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3305 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3306 localParameters.MultiSampleQuality = 0;
3307 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
3308 localParameters.hDeviceWindow = window;
3309 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3310 localParameters.EnableAutoDepthStencil = TRUE;
3311 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16;
3312 localParameters.Flags = 0;
3313 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3314 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3316 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
3318 /* Set this NOW, otherwise creating the depth stencil surface will cause a
3319 * recursive loop until ram or emulated video memory is full
3321 This->d3d_initialized = TRUE;
3323 hr = IWineD3DDevice_Init3D(This->wineD3DDevice,
3324 &localParameters,
3325 D3D7CB_CreateAdditionalSwapChain);
3326 if(FAILED(hr))
3328 This->d3d_target = NULL;
3329 This->d3d_initialized = FALSE;
3330 return hr;
3333 This->declArraySize = 2;
3334 This->decls = HeapAlloc(GetProcessHeap(),
3335 HEAP_ZERO_MEMORY,
3336 sizeof(*This->decls) * This->declArraySize);
3337 if(!This->decls)
3339 ERR("Error allocating an array for the converted vertex decls\n");
3340 This->declArraySize = 0;
3341 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice,
3342 D3D7CB_DestroyDepthStencilSurface,
3343 D3D7CB_DestroySwapChain);
3344 return E_OUTOFMEMORY;
3347 /* Create an Index Buffer parent */
3348 TRACE("(%p) Successfully initialized 3D\n", This);
3349 return DD_OK;
3352 /*****************************************************************************
3353 * DirectDrawCreateClipper (DDRAW.@)
3355 * Creates a new IDirectDrawClipper object.
3357 * Params:
3358 * Clipper: Address to write the interface pointer to
3359 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3360 * NULL
3362 * Returns:
3363 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3364 * E_OUTOFMEMORY if allocating the object failed
3366 *****************************************************************************/
3367 HRESULT WINAPI
3368 DirectDrawCreateClipper(DWORD Flags,
3369 LPDIRECTDRAWCLIPPER *Clipper,
3370 IUnknown *UnkOuter)
3372 IDirectDrawClipperImpl* object;
3373 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
3375 EnterCriticalSection(&ddraw_cs);
3376 if (UnkOuter != NULL)
3378 LeaveCriticalSection(&ddraw_cs);
3379 return CLASS_E_NOAGGREGATION;
3382 if (!LoadWineD3D())
3384 LeaveCriticalSection(&ddraw_cs);
3385 return DDERR_NODIRECTDRAWSUPPORT;
3388 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3389 sizeof(IDirectDrawClipperImpl));
3390 if (object == NULL)
3392 LeaveCriticalSection(&ddraw_cs);
3393 return E_OUTOFMEMORY;
3396 ICOM_INIT_INTERFACE(object, IDirectDrawClipper, IDirectDrawClipper_Vtbl);
3397 object->ref = 1;
3398 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
3399 if(!object->wineD3DClipper)
3401 HeapFree(GetProcessHeap(), 0, object);
3402 LeaveCriticalSection(&ddraw_cs);
3403 return E_OUTOFMEMORY;
3406 *Clipper = (IDirectDrawClipper *) object;
3407 LeaveCriticalSection(&ddraw_cs);
3408 return DD_OK;
3411 /*****************************************************************************
3412 * IDirectDraw7::CreateClipper
3414 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3416 *****************************************************************************/
3417 static HRESULT WINAPI
3418 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3419 DWORD Flags,
3420 IDirectDrawClipper **Clipper,
3421 IUnknown *UnkOuter)
3423 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3424 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3425 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3428 /*****************************************************************************
3429 * IDirectDraw7::CreatePalette
3431 * Creates a new IDirectDrawPalette object
3433 * Params:
3434 * Flags: The flags for the new clipper
3435 * ColorTable: Color table to assign to the new clipper
3436 * Palette: Address to write the interface pointer to
3437 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3438 * NULL
3440 * Returns:
3441 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3442 * E_OUTOFMEMORY if allocating the object failed
3444 *****************************************************************************/
3445 static HRESULT WINAPI
3446 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3447 DWORD Flags,
3448 PALETTEENTRY *ColorTable,
3449 IDirectDrawPalette **Palette,
3450 IUnknown *pUnkOuter)
3452 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3453 IDirectDrawPaletteImpl *object;
3454 HRESULT hr = DDERR_GENERIC;
3455 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3457 EnterCriticalSection(&ddraw_cs);
3458 if(pUnkOuter != NULL)
3460 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3461 LeaveCriticalSection(&ddraw_cs);
3462 return CLASS_E_NOAGGREGATION;
3465 /* The refcount test shows that a cooplevel is required for this */
3466 if(!This->cooperative_level)
3468 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3469 LeaveCriticalSection(&ddraw_cs);
3470 return DDERR_NOCOOPERATIVELEVELSET;
3473 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3474 if(!object)
3476 ERR("Out of memory when allocating memory for a palette implementation\n");
3477 LeaveCriticalSection(&ddraw_cs);
3478 return E_OUTOFMEMORY;
3481 ICOM_INIT_INTERFACE(object, IDirectDrawPalette, IDirectDrawPalette_Vtbl);
3482 object->ref = 1;
3483 object->ddraw_owner = This;
3485 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags, ColorTable, &object->wineD3DPalette, (IUnknown *) ICOM_INTERFACE(object, IDirectDrawPalette) );
3486 if(hr != DD_OK)
3488 HeapFree(GetProcessHeap(), 0, object);
3489 LeaveCriticalSection(&ddraw_cs);
3490 return hr;
3493 IDirectDraw7_AddRef(iface);
3494 object->ifaceToRelease = (IUnknown *) iface;
3495 *Palette = ICOM_INTERFACE(object, IDirectDrawPalette);
3496 LeaveCriticalSection(&ddraw_cs);
3497 return DD_OK;
3500 /*****************************************************************************
3501 * IDirectDraw7::DuplicateSurface
3503 * Duplicates a surface. The surface memory points to the same memory as
3504 * the original surface, and it's released when the last surface referencing
3505 * it is released. I guess that's beyond Wine's surface management right now
3506 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3507 * test application to implement this)
3509 * Params:
3510 * Src: Address of the source surface
3511 * Dest: Address to write the new surface pointer to
3513 * Returns:
3514 * See IDirectDraw7::CreateSurface
3516 *****************************************************************************/
3517 static HRESULT WINAPI
3518 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3519 IDirectDrawSurface7 *Src,
3520 IDirectDrawSurface7 **Dest)
3522 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3523 IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Src);
3525 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3527 /* For now, simply create a new, independent surface */
3528 return IDirectDraw7_CreateSurface(iface,
3529 &Surf->surface_desc,
3530 Dest,
3531 NULL);
3534 /*****************************************************************************
3535 * IDirectDraw7 VTable
3536 *****************************************************************************/
3537 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3539 /*** IUnknown ***/
3540 IDirectDrawImpl_QueryInterface,
3541 IDirectDrawImpl_AddRef,
3542 IDirectDrawImpl_Release,
3543 /*** IDirectDraw ***/
3544 IDirectDrawImpl_Compact,
3545 IDirectDrawImpl_CreateClipper,
3546 IDirectDrawImpl_CreatePalette,
3547 IDirectDrawImpl_CreateSurface,
3548 IDirectDrawImpl_DuplicateSurface,
3549 IDirectDrawImpl_EnumDisplayModes,
3550 IDirectDrawImpl_EnumSurfaces,
3551 IDirectDrawImpl_FlipToGDISurface,
3552 IDirectDrawImpl_GetCaps,
3553 IDirectDrawImpl_GetDisplayMode,
3554 IDirectDrawImpl_GetFourCCCodes,
3555 IDirectDrawImpl_GetGDISurface,
3556 IDirectDrawImpl_GetMonitorFrequency,
3557 IDirectDrawImpl_GetScanLine,
3558 IDirectDrawImpl_GetVerticalBlankStatus,
3559 IDirectDrawImpl_Initialize,
3560 IDirectDrawImpl_RestoreDisplayMode,
3561 IDirectDrawImpl_SetCooperativeLevel,
3562 IDirectDrawImpl_SetDisplayMode,
3563 IDirectDrawImpl_WaitForVerticalBlank,
3564 /*** IDirectDraw2 ***/
3565 IDirectDrawImpl_GetAvailableVidMem,
3566 /*** IDirectDraw7 ***/
3567 IDirectDrawImpl_GetSurfaceFromDC,
3568 IDirectDrawImpl_RestoreAllSurfaces,
3569 IDirectDrawImpl_TestCooperativeLevel,
3570 IDirectDrawImpl_GetDeviceIdentifier,
3571 /*** IDirectDraw7 ***/
3572 IDirectDrawImpl_StartModeTest,
3573 IDirectDrawImpl_EvaluateMode
3576 /*****************************************************************************
3577 * IDirectDrawImpl_FindDecl
3579 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3580 * if none was found.
3582 * This function is in ddraw.c and the DDraw object space because D3D7
3583 * vertex buffers are created using the IDirect3D interface to the ddraw
3584 * object, so they can be valid across D3D devices(theoretically. The ddraw
3585 * object also owns the wined3d device
3587 * Parameters:
3588 * This: Device
3589 * fvf: Fvf to find the decl for
3591 * Returns:
3592 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3593 * fvf otherwise.
3595 *****************************************************************************/
3596 IWineD3DVertexDeclaration *
3597 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3598 DWORD fvf)
3600 HRESULT hr;
3601 IWineD3DVertexDeclaration* pDecl = NULL;
3602 int p, low, high; /* deliberately signed */
3603 struct FvfToDecl *convertedDecls = This->decls;
3605 TRACE("Searching for declaration for fvf %08x... ", fvf);
3607 low = 0;
3608 high = This->numConvertedDecls - 1;
3609 while(low <= high) {
3610 p = (low + high) >> 1;
3611 TRACE("%d ", p);
3612 if(convertedDecls[p].fvf == fvf) {
3613 TRACE("found %p\n", convertedDecls[p].decl);
3614 return convertedDecls[p].decl;
3615 } else if(convertedDecls[p].fvf < fvf) {
3616 low = p + 1;
3617 } else {
3618 high = p - 1;
3621 TRACE("not found. Creating and inserting at position %d.\n", low);
3623 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice,
3624 &pDecl,
3625 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7),
3626 fvf);
3627 if (hr != S_OK) return NULL;
3629 if(This->declArraySize == This->numConvertedDecls) {
3630 int grow = max(This->declArraySize / 2, 8);
3631 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3632 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3633 if(!convertedDecls) {
3634 /* This will destroy it */
3635 IWineD3DVertexDeclaration_Release(pDecl);
3636 return NULL;
3638 This->decls = convertedDecls;
3639 This->declArraySize += grow;
3642 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3643 convertedDecls[low].decl = pDecl;
3644 convertedDecls[low].fvf = fvf;
3645 This->numConvertedDecls++;
3647 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
3648 return pDecl;