push 16813bf814fcb5d03ddef80ecfd11fc0f25812b5
[wine/hacks.git] / dlls / ddraw / ddraw.c
blobe9303bd4911e8bd285f0403f5858aa13073fb32f
1 /*
2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger
6 * Copyright 2008 Denver Gingerich
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h>
31 #define COBJMACROS
32 #define NONAMELESSUNION
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "wingdi.h"
38 #include "winreg.h"
39 #include "wine/exception.h"
41 #include "ddraw.h"
42 #include "d3d.h"
44 #include "ddraw_private.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
49 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
51 static BOOL IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested, const DDSURFACEDESC2* provided);
52 static HRESULT WINAPI IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
53 static HRESULT WINAPI IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This, DDSURFACEDESC2 *pDDSD, IDirectDrawSurfaceImpl **ppSurf, UINT level);
54 static HRESULT WINAPI IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This, IDirectDrawSurfaceImpl *primary);
56 /* Device identifier. Don't relay it to WineD3D */
57 static const DDDEVICEIDENTIFIER2 deviceidentifier =
59 "display",
60 "DirectDraw HAL",
61 { { 0x00010001, 0x00010001 } },
62 0, 0, 0, 0,
63 /* a8373c10-7ac4-4deb-849a-009844d08b2d */
64 {0xa8373c10,0x7ac4,0x4deb, {0x84,0x9a,0x00,0x98,0x44,0xd0,0x8b,0x2d}},
68 /*****************************************************************************
69 * IUnknown Methods
70 *****************************************************************************/
72 /*****************************************************************************
73 * IDirectDraw7::QueryInterface
75 * Queries different interfaces of the DirectDraw object. It can return
76 * IDirectDraw interfaces in version 1, 2, 4 and 7, and IDirect3D interfaces
77 * in version 1, 2, 3 and 7. An IDirect3DDevice can be created with this
78 * method.
79 * The returned interface is AddRef()-ed before it's returned
81 * Used for version 1, 2, 4 and 7
83 * Params:
84 * refiid: Interface ID asked for
85 * obj: Used to return the interface pointer
87 * Returns:
88 * S_OK if an interface was found
89 * E_NOINTERFACE if the requested interface wasn't found
91 *****************************************************************************/
92 static HRESULT WINAPI
93 IDirectDrawImpl_QueryInterface(IDirectDraw7 *iface,
94 REFIID refiid,
95 void **obj)
97 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
99 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
101 /* Can change surface impl type */
102 EnterCriticalSection(&ddraw_cs);
104 /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
105 *obj = NULL;
107 if(!refiid)
109 LeaveCriticalSection(&ddraw_cs);
110 return DDERR_INVALIDPARAMS;
113 /* Check DirectDraw Interfaces */
114 if ( IsEqualGUID( &IID_IUnknown, refiid ) ||
115 IsEqualGUID( &IID_IDirectDraw7, refiid ) )
117 *obj = ICOM_INTERFACE(This, IDirectDraw7);
118 TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
120 else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
122 *obj = ICOM_INTERFACE(This, IDirectDraw4);
123 TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
125 else if ( IsEqualGUID( &IID_IDirectDraw3, refiid ) )
127 *obj = ICOM_INTERFACE(This, IDirectDraw3);
128 TRACE("(%p) Returning IDirectDraw3 interface at %p\n", This, *obj);
130 else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
132 *obj = ICOM_INTERFACE(This, IDirectDraw2);
133 TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
135 else if ( IsEqualGUID( &IID_IDirectDraw, refiid ) )
137 *obj = ICOM_INTERFACE(This, IDirectDraw);
138 TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
141 /* Direct3D
142 * The refcount unit test revealed that an IDirect3D7 interface can only be queried
143 * from a DirectDraw object that was created as an IDirectDraw7 interface. No idea
144 * who had this idea and why. The older interfaces can query and IDirect3D version
145 * because they are all created as IDirectDraw(1). This isn't really crucial behavior,
146 * and messy to implement with the common creation function, so it has been left out here.
148 else if ( IsEqualGUID( &IID_IDirect3D , refiid ) ||
149 IsEqualGUID( &IID_IDirect3D2 , refiid ) ||
150 IsEqualGUID( &IID_IDirect3D3 , refiid ) ||
151 IsEqualGUID( &IID_IDirect3D7 , refiid ) )
153 /* Check the surface implementation */
154 if(This->ImplType == SURFACE_UNKNOWN)
156 /* Apps may create the IDirect3D Interface before the primary surface.
157 * set the surface implementation */
158 This->ImplType = SURFACE_OPENGL;
159 TRACE("(%p) Choosing OpenGL surfaces because a Direct3D interface was requested\n", This);
161 else if(This->ImplType != SURFACE_OPENGL && DefaultSurfaceType == SURFACE_UNKNOWN)
163 ERR("(%p) The App is requesting a D3D device, but a non-OpenGL surface type was choosen. Prepare for trouble!\n", This);
164 ERR(" (%p) You may want to contact wine-devel for help\n", This);
165 /* Should I assert(0) here??? */
167 else if(This->ImplType != SURFACE_OPENGL)
169 WARN("The app requests a Direct3D interface, but non-opengl surfaces where set in winecfg\n");
170 /* Do not abort here, only reject 3D Device creation */
173 if ( IsEqualGUID( &IID_IDirect3D , refiid ) )
175 This->d3dversion = 1;
176 *obj = ICOM_INTERFACE(This, IDirect3D);
177 TRACE(" returning Direct3D interface at %p.\n", *obj);
179 else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
181 This->d3dversion = 2;
182 *obj = ICOM_INTERFACE(This, IDirect3D2);
183 TRACE(" returning Direct3D2 interface at %p.\n", *obj);
185 else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
187 This->d3dversion = 3;
188 *obj = ICOM_INTERFACE(This, IDirect3D3);
189 TRACE(" returning Direct3D3 interface at %p.\n", *obj);
191 else if(IsEqualGUID( &IID_IDirect3D7 , refiid ))
193 This->d3dversion = 7;
194 *obj = ICOM_INTERFACE(This, IDirect3D7);
195 TRACE(" returning Direct3D7 interface at %p.\n", *obj);
199 /* Unknown interface */
200 else
202 ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
203 LeaveCriticalSection(&ddraw_cs);
204 return E_NOINTERFACE;
207 IUnknown_AddRef( (IUnknown *) *obj );
208 LeaveCriticalSection(&ddraw_cs);
209 return S_OK;
212 /*****************************************************************************
213 * IDirectDraw7::AddRef
215 * Increases the interfaces refcount, basically
217 * DDraw refcounting is a bit tricky. The different DirectDraw interface
218 * versions have individual refcounts, but the IDirect3D interfaces do not.
219 * All interfaces are from one object, that means calling QueryInterface on an
220 * IDirectDraw7 interface for an IDirectDraw4 interface does not create a new
221 * IDirectDrawImpl object.
223 * That means all AddRef and Release implementations of IDirectDrawX work
224 * with their own counter, and IDirect3DX::AddRef thunk to IDirectDraw (1),
225 * except of IDirect3D7 which thunks to IDirectDraw7
227 * Returns: The new refcount
229 *****************************************************************************/
230 static ULONG WINAPI
231 IDirectDrawImpl_AddRef(IDirectDraw7 *iface)
233 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
234 ULONG ref = InterlockedIncrement(&This->ref7);
236 TRACE("(%p) : incrementing IDirectDraw7 refcount from %u.\n", This, ref -1);
238 if(ref == 1) InterlockedIncrement(&This->numIfaces);
240 return ref;
243 /*****************************************************************************
244 * IDirectDrawImpl_Destroy
246 * Destroys a ddraw object if all refcounts are 0. This is to share code
247 * between the IDirectDrawX::Release functions
249 * Params:
250 * This: DirectDraw object to destroy
252 *****************************************************************************/
253 void
254 IDirectDrawImpl_Destroy(IDirectDrawImpl *This)
256 /* Clear the cooplevel to restore window and display mode */
257 IDirectDraw7_SetCooperativeLevel(ICOM_INTERFACE(This, IDirectDraw7),
258 NULL,
259 DDSCL_NORMAL);
261 /* Destroy the device window if we created one */
262 if(This->devicewindow != 0)
264 TRACE(" (%p) Destroying the device window %p\n", This, This->devicewindow);
265 DestroyWindow(This->devicewindow);
266 This->devicewindow = 0;
269 /* Unregister the window class */
270 UnregisterClassA(This->classname, 0);
272 EnterCriticalSection(&ddraw_cs);
273 list_remove(&This->ddraw_list_entry);
274 LeaveCriticalSection(&ddraw_cs);
276 /* Release the attached WineD3D stuff */
277 IWineD3DDevice_Release(This->wineD3DDevice);
278 IWineD3D_Release(This->wineD3D);
280 /* Now free the object */
281 HeapFree(GetProcessHeap(), 0, This);
284 /*****************************************************************************
285 * IDirectDraw7::Release
287 * Decreases the refcount. If the refcount falls to 0, the object is destroyed
289 * Returns: The new refcount
290 *****************************************************************************/
291 static ULONG WINAPI
292 IDirectDrawImpl_Release(IDirectDraw7 *iface)
294 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
295 ULONG ref = InterlockedDecrement(&This->ref7);
297 TRACE("(%p)->() decrementing IDirectDraw7 refcount from %u.\n", This, ref +1);
299 if(ref == 0)
301 ULONG ifacecount = InterlockedDecrement(&This->numIfaces);
302 if(ifacecount == 0) IDirectDrawImpl_Destroy(This);
305 return ref;
308 /*****************************************************************************
309 * IDirectDraw methods
310 *****************************************************************************/
312 /*****************************************************************************
313 * IDirectDraw7::SetCooperativeLevel
315 * Sets the cooperative level for the DirectDraw object, and the window
316 * assigned to it. The cooperative level determines the general behavior
317 * of the DirectDraw application
319 * Warning: This is quite tricky, as it's not really documented which
320 * cooperative levels can be combined with each other. If a game fails
321 * after this function, try to check the cooperative levels passed on
322 * Windows, and if it returns something different.
324 * If you think that this function caused the failure because it writes a
325 * fixme, be sure to run again with a +ddraw trace.
327 * What is known about cooperative levels (See the ddraw modes test):
328 * DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN must be used with each other
329 * DDSCL_NORMAL is not compatible with DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN
330 * DDSCL_SETFOCUSWINDOW can be passed only in DDSCL_NORMAL mode, but after that
331 * DDSCL_FULLSCREEN can be activated
332 * DDSCL_SETFOCUSWINDOW may only be used with DDSCL_NOWINDOWCHANGES
334 * Handled flags: DDSCL_NORMAL, DDSCL_FULLSCREEN, DDSCL_EXCLUSIVE,
335 * DDSCL_SETFOCUSWINDOW (partially),
336 * DDSCL_MULTITHREADED (work in progress)
338 * Unhandled flags, which should be implemented
339 * DDSCL_SETDEVICEWINDOW: Sets a window specially used for rendering (I don't
340 * expect any difference to a normal window for wine)
341 * DDSCL_CREATEDEVICEWINDOW: Tells ddraw to create its own window for
342 * rendering (Possible test case: Half-life)
344 * Unsure about these: DDSCL_FPUSETUP DDSCL_FPURESERVE
346 * These don't seem very important for wine:
347 * DDSCL_ALLOWREBOOT, DDSCL_NOWINDOWCHANGES, DDSCL_ALLOWMODEX
349 * Returns:
350 * DD_OK if the cooperative level was set successfully
351 * DDERR_INVALIDPARAMS if the passed cooperative level combination is invalid
352 * DDERR_HWNDALREADYSET if DDSCL_SETFOCUSWINDOW is passed in exclusive mode
353 * (Probably others too, have to investigate)
355 *****************************************************************************/
356 static HRESULT WINAPI
357 IDirectDrawImpl_SetCooperativeLevel(IDirectDraw7 *iface,
358 HWND hwnd,
359 DWORD cooplevel)
361 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
362 HWND window;
364 TRACE("(%p)->(%p,%08x)\n",This,hwnd,cooplevel);
365 DDRAW_dump_cooperativelevel(cooplevel);
367 EnterCriticalSection(&ddraw_cs);
369 /* Get the old window */
370 window = This->dest_window;
372 /* Tests suggest that we need one of them: */
373 if(!(cooplevel & (DDSCL_SETFOCUSWINDOW |
374 DDSCL_NORMAL |
375 DDSCL_EXCLUSIVE )))
377 TRACE("Incorrect cooplevel flags, returning DDERR_INVALIDPARAMS\n");
378 LeaveCriticalSection(&ddraw_cs);
379 return DDERR_INVALIDPARAMS;
382 /* Handle those levels first which set various hwnds */
383 if(cooplevel & DDSCL_SETFOCUSWINDOW)
385 /* This isn't compatible with a lot of flags */
386 if(cooplevel & ( DDSCL_MULTITHREADED |
387 DDSCL_FPUSETUP |
388 DDSCL_FPUPRESERVE |
389 DDSCL_ALLOWREBOOT |
390 DDSCL_ALLOWMODEX |
391 DDSCL_SETDEVICEWINDOW |
392 DDSCL_NORMAL |
393 DDSCL_EXCLUSIVE |
394 DDSCL_FULLSCREEN ) )
396 TRACE("Called with incompatible flags, returning DDERR_INVALIDPARAMS\n");
397 LeaveCriticalSection(&ddraw_cs);
398 return DDERR_INVALIDPARAMS;
400 else if( (This->cooperative_level & DDSCL_FULLSCREEN) && window)
402 TRACE("Setting DDSCL_SETFOCUSWINDOW with an already set window, returning DDERR_HWNDALREADYSET\n");
403 LeaveCriticalSection(&ddraw_cs);
404 return DDERR_HWNDALREADYSET;
407 This->focuswindow = hwnd;
408 /* Won't use the hwnd param for anything else */
409 hwnd = NULL;
411 /* Use the focus window for drawing too */
412 This->dest_window = This->focuswindow;
414 /* Destroy the device window, if we have one */
415 if(This->devicewindow)
417 DestroyWindow(This->devicewindow);
418 This->devicewindow = NULL;
421 /* DDSCL_NORMAL or DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE */
422 if(cooplevel & DDSCL_NORMAL)
424 /* Can't coexist with fullscreen or exclusive */
425 if(cooplevel & (DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE) )
427 TRACE("(%p) DDSCL_NORMAL is not compative with DDSCL_FULLSCREEN or DDSCL_EXCLUSIVE\n", This);
428 LeaveCriticalSection(&ddraw_cs);
429 return DDERR_INVALIDPARAMS;
432 /* Switching from fullscreen? */
433 if(This->cooperative_level & DDSCL_FULLSCREEN)
435 /* Restore the display mode */
436 IDirectDraw7_RestoreDisplayMode(iface);
438 This->cooperative_level &= ~DDSCL_FULLSCREEN;
439 This->cooperative_level &= ~DDSCL_EXCLUSIVE;
440 This->cooperative_level &= ~DDSCL_ALLOWMODEX;
443 /* Don't override focus windows or private device windows */
444 if( hwnd &&
445 !(This->focuswindow) &&
446 !(This->devicewindow) &&
447 (hwnd != window) )
449 This->dest_window = hwnd;
452 else if(cooplevel & DDSCL_FULLSCREEN)
454 /* Needs DDSCL_EXCLUSIVE */
455 if(!(cooplevel & DDSCL_EXCLUSIVE) )
457 TRACE("(%p) DDSCL_FULLSCREEN needs DDSCL_EXCLUSIVE\n", This);
458 LeaveCriticalSection(&ddraw_cs);
459 return DDERR_INVALIDPARAMS;
461 /* Need a HWND
462 if(hwnd == 0)
464 TRACE("(%p) DDSCL_FULLSCREEN needs a HWND\n", This);
465 return DDERR_INVALIDPARAMS;
469 This->cooperative_level &= ~DDSCL_NORMAL;
471 /* Don't override focus windows or private device windows */
472 if( hwnd &&
473 !(This->focuswindow) &&
474 !(This->devicewindow) &&
475 (hwnd != window) )
477 BYTE buffer[32];
478 DWORD size = sizeof(buffer);
479 HKEY hkey = 0;
480 HWND drawwin = hwnd;
481 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
482 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey)) {
483 if (!RegQueryValueExA( hkey, "DirectDrawDesktopHack", 0, NULL, buffer, &size)) {
484 if ( IS_OPTION_TRUE( buffer[0] ) ) {
485 TRACE("Enabling DirectDrawDesktopHack hack\n");
486 drawwin = GetDesktopWindow();
490 This->dest_window = drawwin;
493 else if(cooplevel & DDSCL_EXCLUSIVE)
495 TRACE("(%p) DDSCL_EXCLUSIVE needs DDSCL_FULLSCREEN\n", This);
496 LeaveCriticalSection(&ddraw_cs);
497 return DDERR_INVALIDPARAMS;
500 if(cooplevel & DDSCL_CREATEDEVICEWINDOW)
502 /* Don't create a device window if a focus window is set */
503 if( !(This->focuswindow) )
505 HWND devicewindow = CreateWindowExA(0, This->classname, "DDraw device window",
506 WS_POPUP, 0, 0,
507 GetSystemMetrics(SM_CXSCREEN),
508 GetSystemMetrics(SM_CYSCREEN),
509 NULL, NULL, GetModuleHandleA(0), NULL);
511 ShowWindow(devicewindow, SW_SHOW); /* Just to be sure */
512 TRACE("(%p) Created a DDraw device window. HWND=%p\n", This, devicewindow);
514 This->devicewindow = devicewindow;
515 This->dest_window = devicewindow;
519 if(cooplevel & DDSCL_MULTITHREADED && !(This->cooperative_level & DDSCL_MULTITHREADED))
521 /* Enable thread safety in wined3d */
522 IWineD3DDevice_SetMultithreaded(This->wineD3DDevice);
525 /* Unhandled flags */
526 if(cooplevel & DDSCL_ALLOWREBOOT)
527 WARN("(%p) Unhandled flag DDSCL_ALLOWREBOOT, harmless\n", This);
528 if(cooplevel & DDSCL_ALLOWMODEX)
529 WARN("(%p) Unhandled flag DDSCL_ALLOWMODEX, harmless\n", This);
530 if(cooplevel & DDSCL_FPUSETUP)
531 WARN("(%p) Unhandled flag DDSCL_FPUSETUP, harmless\n", This);
533 /* Store the cooperative_level */
534 This->cooperative_level |= cooplevel;
535 TRACE("SetCooperativeLevel retuning DD_OK\n");
536 LeaveCriticalSection(&ddraw_cs);
537 return DD_OK;
540 /*****************************************************************************
542 * Helper function for SetDisplayMode and RestoreDisplayMode
544 * Implements DirectDraw's SetDisplayMode, but ignores the value of
545 * ForceRefreshRate, since it is already handled by
546 * IDirectDrawImpl_SetDisplayMode. RestoreDisplayMode can use this function
547 * without worrying that ForceRefreshRate will override the refresh rate. For
548 * argument and return value documentation, see
549 * IDirectDrawImpl_SetDisplayMode.
551 *****************************************************************************/
552 static HRESULT
553 IDirectDrawImpl_SetDisplayModeNoOverride(IDirectDraw7 *iface,
554 DWORD Width,
555 DWORD Height,
556 DWORD BPP,
557 DWORD RefreshRate,
558 DWORD Flags)
560 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
561 WINED3DDISPLAYMODE Mode;
562 HRESULT hr;
563 TRACE("(%p)->(%d,%d,%d,%d,%x: Relay!\n", This, Width, Height, BPP, RefreshRate, Flags);
565 EnterCriticalSection(&ddraw_cs);
566 if( !Width || !Height )
568 ERR("Width=%d, Height=%d, what to do?\n", Width, Height);
569 /* It looks like Need for Speed Porsche Unleashed expects DD_OK here */
570 LeaveCriticalSection(&ddraw_cs);
571 return DD_OK;
574 /* Check the exclusive mode
575 if(!(This->cooperative_level & DDSCL_EXCLUSIVE))
576 return DDERR_NOEXCLUSIVEMODE;
577 * This is WRONG. Don't know if the SDK is completely
578 * wrong and if there are any conditions when DDERR_NOEXCLUSIVE
579 * is returned, but Half-Life 1.1.1.1 (Steam version)
580 * depends on this
583 Mode.Width = Width;
584 Mode.Height = Height;
585 Mode.RefreshRate = RefreshRate;
586 switch(BPP)
588 case 8: Mode.Format = WINED3DFMT_P8; break;
589 case 15: Mode.Format = WINED3DFMT_X1R5G5B5; break;
590 case 16: Mode.Format = WINED3DFMT_R5G6B5; break;
591 case 24: Mode.Format = WINED3DFMT_R8G8B8; break;
592 case 32: Mode.Format = WINED3DFMT_X8R8G8B8; break;
595 /* TODO: The possible return values from msdn suggest that
596 * the screen mode can't be changed if a surface is locked
597 * or some drawing is in progress
600 /* TODO: Lose the primary surface */
601 hr = IWineD3DDevice_SetDisplayMode(This->wineD3DDevice,
602 0, /* First swapchain */
603 &Mode);
604 LeaveCriticalSection(&ddraw_cs);
605 switch(hr)
607 case WINED3DERR_NOTAVAILABLE: return DDERR_UNSUPPORTED;
608 default: return hr;
612 /*****************************************************************************
613 * IDirectDraw7::SetDisplayMode
615 * Sets the display screen resolution, color depth and refresh frequency
616 * when in fullscreen mode (in theory).
617 * Possible return values listed in the SDK suggest that this method fails
618 * when not in fullscreen mode, but this is wrong. Windows 2000 happily sets
619 * the display mode in DDSCL_NORMAL mode without an hwnd specified.
620 * It seems to be valid to pass 0 for With and Height, this has to be tested
621 * It could mean that the current video mode should be left as-is. (But why
622 * call it then?)
624 * Params:
625 * Height, Width: Screen dimension
626 * BPP: Color depth in Bits per pixel
627 * Refreshrate: Screen refresh rate
628 * Flags: Other stuff
630 * Returns
631 * DD_OK on success
633 *****************************************************************************/
634 static HRESULT WINAPI
635 IDirectDrawImpl_SetDisplayMode(IDirectDraw7 *iface,
636 DWORD Width,
637 DWORD Height,
638 DWORD BPP,
639 DWORD RefreshRate,
640 DWORD Flags)
642 if (force_refresh_rate != 0)
644 TRACE("ForceRefreshRate overriding passed-in refresh rate (%d Hz) to %d Hz\n", RefreshRate, force_refresh_rate);
645 RefreshRate = force_refresh_rate;
648 return IDirectDrawImpl_SetDisplayModeNoOverride(iface, Width, Height, BPP,
649 RefreshRate, Flags);
652 /*****************************************************************************
653 * IDirectDraw7::RestoreDisplayMode
655 * Restores the display mode to what it was at creation time. Basically.
657 * A problem arises when there are 2 DirectDraw objects using the same hwnd:
658 * -> DD_1 finds the screen at 1400x1050x32 when created, sets it to 640x480x16
659 * -> DD_2 is created, finds the screen at 640x480x16, sets it to 1024x768x32
660 * -> DD_1 is released. The screen should be left at 1024x768x32.
661 * -> DD_2 is released. The screen should be set to 1400x1050x32
662 * This case is unhandled right now, but Empire Earth does it this way.
663 * (But perhaps there is something in SetCooperativeLevel to prevent this)
665 * The msdn says that this method resets the display mode to what it was before
666 * SetDisplayMode was called. What if SetDisplayModes is called 2 times??
668 * Returns
669 * DD_OK on success
670 * DDERR_NOEXCLUSIVE mode if the device isn't in fullscreen mode
672 *****************************************************************************/
673 static HRESULT WINAPI
674 IDirectDrawImpl_RestoreDisplayMode(IDirectDraw7 *iface)
676 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
677 TRACE("(%p)\n", This);
679 return IDirectDrawImpl_SetDisplayModeNoOverride(ICOM_INTERFACE(This, IDirectDraw7),
680 This->orig_width,
681 This->orig_height,
682 This->orig_bpp,
687 /*****************************************************************************
688 * IDirectDraw7::GetCaps
690 * Returns the drives capabilities
692 * Used for version 1, 2, 4 and 7
694 * Params:
695 * DriverCaps: Structure to write the Hardware accelerated caps to
696 * HelCaps: Structure to write the emulation caps to
698 * Returns
699 * This implementation returns DD_OK only
701 *****************************************************************************/
702 static HRESULT WINAPI
703 IDirectDrawImpl_GetCaps(IDirectDraw7 *iface,
704 DDCAPS *DriverCaps,
705 DDCAPS *HELCaps)
707 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
708 DDCAPS caps;
709 WINED3DCAPS winecaps;
710 HRESULT hr;
711 DDSCAPS2 ddscaps = {0, 0, 0, 0};
712 TRACE("(%p)->(%p,%p)\n", This, DriverCaps, HELCaps);
714 /* One structure must be != NULL */
715 if( (!DriverCaps) && (!HELCaps) )
717 ERR("(%p) Invalid params to IDirectDrawImpl_GetCaps\n", This);
718 return DDERR_INVALIDPARAMS;
721 memset(&caps, 0, sizeof(caps));
722 memset(&winecaps, 0, sizeof(winecaps));
723 caps.dwSize = sizeof(caps);
724 EnterCriticalSection(&ddraw_cs);
725 hr = IWineD3DDevice_GetDeviceCaps(This->wineD3DDevice, &winecaps);
726 if(FAILED(hr)) {
727 WARN("IWineD3DDevice::GetDeviceCaps failed\n");
728 LeaveCriticalSection(&ddraw_cs);
729 return hr;
732 hr = IDirectDraw7_GetAvailableVidMem(iface, &ddscaps, &caps.dwVidMemTotal, &caps.dwVidMemFree);
733 LeaveCriticalSection(&ddraw_cs);
734 if(FAILED(hr)) {
735 WARN("IDirectDraw7::GetAvailableVidMem failed\n");
736 return hr;
739 caps.dwCaps = winecaps.DirectDrawCaps.Caps;
740 caps.dwCaps2 = winecaps.DirectDrawCaps.Caps2;
741 caps.dwCKeyCaps = winecaps.DirectDrawCaps.CKeyCaps;
742 caps.dwFXCaps = winecaps.DirectDrawCaps.FXCaps;
743 caps.dwPalCaps = winecaps.DirectDrawCaps.PalCaps;
744 caps.ddsCaps.dwCaps = winecaps.DirectDrawCaps.ddsCaps;
745 caps.dwSVBCaps = winecaps.DirectDrawCaps.SVBCaps;
746 caps.dwSVBCKeyCaps = winecaps.DirectDrawCaps.SVBCKeyCaps;
747 caps.dwSVBFXCaps = winecaps.DirectDrawCaps.SVBFXCaps;
748 caps.dwVSBCaps = winecaps.DirectDrawCaps.VSBCaps;
749 caps.dwVSBCKeyCaps = winecaps.DirectDrawCaps.VSBCKeyCaps;
750 caps.dwVSBFXCaps = winecaps.DirectDrawCaps.VSBFXCaps;
751 caps.dwSSBCaps = winecaps.DirectDrawCaps.SSBCaps;
752 caps.dwSSBCKeyCaps = winecaps.DirectDrawCaps.SSBCKeyCaps;
753 caps.dwSSBFXCaps = winecaps.DirectDrawCaps.SSBFXCaps;
755 /* Even if WineD3D supports 3D rendering, remove the cap if ddraw is configured
756 * not to use it
758 if(DefaultSurfaceType == SURFACE_GDI) {
759 caps.dwCaps &= ~DDCAPS_3D;
760 caps.ddsCaps.dwCaps &= ~(DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER);
762 if(winecaps.DirectDrawCaps.StrideAlign != 0) {
763 caps.dwCaps |= DDCAPS_ALIGNSTRIDE;
764 caps.dwAlignStrideAlign = winecaps.DirectDrawCaps.StrideAlign;
767 if(DriverCaps)
769 DD_STRUCT_COPY_BYSIZE(DriverCaps, &caps);
770 if (TRACE_ON(ddraw))
772 TRACE("Driver Caps :\n");
773 DDRAW_dump_DDCAPS(DriverCaps);
777 if(HELCaps)
779 DD_STRUCT_COPY_BYSIZE(HELCaps, &caps);
780 if (TRACE_ON(ddraw))
782 TRACE("HEL Caps :\n");
783 DDRAW_dump_DDCAPS(HELCaps);
787 return DD_OK;
790 /*****************************************************************************
791 * IDirectDraw7::Compact
793 * No idea what it does, MSDN says it's not implemented.
795 * Returns
796 * DD_OK, but this is unchecked
798 *****************************************************************************/
799 static HRESULT WINAPI
800 IDirectDrawImpl_Compact(IDirectDraw7 *iface)
802 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
803 TRACE("(%p)\n", This);
805 return DD_OK;
808 /*****************************************************************************
809 * IDirectDraw7::GetDisplayMode
811 * Returns information about the current display mode
813 * Exists in Version 1, 2, 4 and 7
815 * Params:
816 * DDSD: Address of a surface description structure to write the info to
818 * Returns
819 * DD_OK
821 *****************************************************************************/
822 static HRESULT WINAPI
823 IDirectDrawImpl_GetDisplayMode(IDirectDraw7 *iface,
824 DDSURFACEDESC2 *DDSD)
826 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
827 HRESULT hr;
828 WINED3DDISPLAYMODE Mode;
829 DWORD Size;
830 TRACE("(%p)->(%p): Relay\n", This, DDSD);
832 EnterCriticalSection(&ddraw_cs);
833 /* This seems sane */
834 if(!DDSD)
836 LeaveCriticalSection(&ddraw_cs);
837 return DDERR_INVALIDPARAMS;
840 /* The necessary members of LPDDSURFACEDESC and LPDDSURFACEDESC2 are equal,
841 * so one method can be used for all versions (Hopefully)
843 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
844 0 /* swapchain 0 */,
845 &Mode);
846 if( hr != D3D_OK )
848 ERR(" (%p) IWineD3DDevice::GetDisplayMode returned %08x\n", This, hr);
849 LeaveCriticalSection(&ddraw_cs);
850 return hr;
853 Size = DDSD->dwSize;
854 memset(DDSD, 0, Size);
856 DDSD->dwSize = Size;
857 DDSD->dwFlags |= DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_REFRESHRATE;
858 DDSD->dwWidth = Mode.Width;
859 DDSD->dwHeight = Mode.Height;
860 DDSD->u2.dwRefreshRate = 60;
861 DDSD->ddsCaps.dwCaps = 0;
862 DDSD->u4.ddpfPixelFormat.dwSize = sizeof(DDSD->u4.ddpfPixelFormat);
863 PixelFormat_WineD3DtoDD(&DDSD->u4.ddpfPixelFormat, Mode.Format);
864 DDSD->u1.lPitch = Mode.Width * DDSD->u4.ddpfPixelFormat.u1.dwRGBBitCount / 8;
866 if(TRACE_ON(ddraw))
868 TRACE("Returning surface desc :\n");
869 DDRAW_dump_surface_desc(DDSD);
872 LeaveCriticalSection(&ddraw_cs);
873 return DD_OK;
876 /*****************************************************************************
877 * IDirectDraw7::GetFourCCCodes
879 * Returns an array of supported FourCC codes.
881 * Exists in Version 1, 2, 4 and 7
883 * Params:
884 * NumCodes: Contains the number of Codes that Codes can carry. Returns the number
885 * of enumerated codes
886 * Codes: Pointer to an array of DWORDs where the supported codes are written
887 * to
889 * Returns
890 * Always returns DD_OK, as it's a stub for now
892 *****************************************************************************/
893 static HRESULT WINAPI
894 IDirectDrawImpl_GetFourCCCodes(IDirectDraw7 *iface,
895 DWORD *NumCodes, DWORD *Codes)
897 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
898 WINED3DFORMAT formats[] = {
899 WINED3DFMT_YUY2, WINED3DFMT_UYVY, WINED3DFMT_YV12,
900 WINED3DFMT_DXT1, WINED3DFMT_DXT2, WINED3DFMT_DXT3, WINED3DFMT_DXT4, WINED3DFMT_DXT5,
901 WINED3DFMT_ATI2N, WINED3DFMT_NVHU, WINED3DFMT_NVHS
903 DWORD count = 0, i, outsize;
904 HRESULT hr;
905 WINED3DDISPLAYMODE d3ddm;
906 WINED3DSURFTYPE type = This->ImplType;
907 TRACE("(%p)->(%p, %p)\n", This, NumCodes, Codes);
909 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
910 0 /* swapchain 0 */,
911 &d3ddm);
913 outsize = NumCodes && Codes ? *NumCodes : 0;
915 if(type == SURFACE_UNKNOWN) type = SURFACE_GDI;
917 for(i = 0; i < (sizeof(formats) / sizeof(formats[0])); i++) {
918 hr = IWineD3D_CheckDeviceFormat(This->wineD3D,
919 WINED3DADAPTER_DEFAULT,
920 WINED3DDEVTYPE_HAL,
921 d3ddm.Format /* AdapterFormat */,
922 0 /* usage */,
923 WINED3DRTYPE_SURFACE,
924 formats[i],
925 type);
926 if(SUCCEEDED(hr)) {
927 if(count < outsize) {
928 Codes[count] = formats[i];
930 count++;
933 if(NumCodes) {
934 TRACE("Returning %u FourCC codes\n", count);
935 *NumCodes = count;
938 return DD_OK;
941 /*****************************************************************************
942 * IDirectDraw7::GetMonitorFrequency
944 * Returns the monitor's frequency
946 * Exists in Version 1, 2, 4 and 7
948 * Params:
949 * Freq: Pointer to a DWORD to write the frequency to
951 * Returns
952 * Always returns DD_OK
954 *****************************************************************************/
955 static HRESULT WINAPI
956 IDirectDrawImpl_GetMonitorFrequency(IDirectDraw7 *iface,
957 DWORD *Freq)
959 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
960 TRACE("(%p)->(%p)\n", This, Freq);
962 /* Ideally this should be in WineD3D, as it concerns the screen setup,
963 * but for now this should make the games happy
965 *Freq = 60;
966 return DD_OK;
969 /*****************************************************************************
970 * IDirectDraw7::GetVerticalBlankStatus
972 * Returns the Vertical blank status of the monitor. This should be in WineD3D
973 * too basically, but as it's a semi stub, I didn't create a function there
975 * Params:
976 * status: Pointer to a BOOL to be filled with the vertical blank status
978 * Returns
979 * DD_OK on success
980 * DDERR_INVALIDPARAMS if status is NULL
982 *****************************************************************************/
983 static HRESULT WINAPI
984 IDirectDrawImpl_GetVerticalBlankStatus(IDirectDraw7 *iface,
985 BOOL *status)
987 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
988 TRACE("(%p)->(%p)\n", This, status);
990 /* This looks sane, the MSDN suggests it too */
991 EnterCriticalSection(&ddraw_cs);
992 if(!status)
994 LeaveCriticalSection(&ddraw_cs);
995 return DDERR_INVALIDPARAMS;
998 *status = This->fake_vblank;
999 This->fake_vblank = !This->fake_vblank;
1000 LeaveCriticalSection(&ddraw_cs);
1001 return DD_OK;
1004 /*****************************************************************************
1005 * IDirectDraw7::GetAvailableVidMem
1007 * Returns the total and free video memory
1009 * Params:
1010 * Caps: Specifies the memory type asked for
1011 * total: Pointer to a DWORD to be filled with the total memory
1012 * free: Pointer to a DWORD to be filled with the free memory
1014 * Returns
1015 * DD_OK on success
1016 * DDERR_INVALIDPARAMS of free and total are NULL
1018 *****************************************************************************/
1019 static HRESULT WINAPI
1020 IDirectDrawImpl_GetAvailableVidMem(IDirectDraw7 *iface, DDSCAPS2 *Caps, DWORD *total, DWORD *free)
1022 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1023 TRACE("(%p)->(%p, %p, %p)\n", This, Caps, total, free);
1025 if(TRACE_ON(ddraw))
1027 TRACE("(%p) Asked for memory with description: ", This);
1028 DDRAW_dump_DDSCAPS2(Caps);
1030 EnterCriticalSection(&ddraw_cs);
1032 /* Todo: System memory vs local video memory vs non-local video memory
1033 * The MSDN also mentions differences between texture memory and other
1034 * resources, but that's not important
1037 if( (!total) && (!free) )
1039 LeaveCriticalSection(&ddraw_cs);
1040 return DDERR_INVALIDPARAMS;
1043 if(total) *total = This->total_vidmem;
1044 if(free) *free = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
1046 LeaveCriticalSection(&ddraw_cs);
1047 return DD_OK;
1050 /*****************************************************************************
1051 * IDirectDraw7::Initialize
1053 * Initializes a DirectDraw interface.
1055 * Params:
1056 * GUID: Interface identifier. Well, don't know what this is really good
1057 * for
1059 * Returns
1060 * Returns DD_OK on the first call,
1061 * DDERR_ALREADYINITIALIZED on repeated calls
1063 *****************************************************************************/
1064 static HRESULT WINAPI
1065 IDirectDrawImpl_Initialize(IDirectDraw7 *iface,
1066 GUID *Guid)
1068 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1069 TRACE("(%p)->(%s): No-op\n", This, debugstr_guid(Guid));
1071 if(This->initialized)
1073 return DDERR_ALREADYINITIALIZED;
1075 else
1077 return DD_OK;
1081 /*****************************************************************************
1082 * IDirectDraw7::FlipToGDISurface
1084 * "Makes the surface that the GDI writes to the primary surface"
1085 * Looks like some windows specific thing we don't have to care about.
1086 * According to MSDN it permits GDI dialog boxes in FULLSCREEN mode. Good to
1087 * show error boxes ;)
1088 * Well, just return DD_OK.
1090 * Returns:
1091 * Always returns DD_OK
1093 *****************************************************************************/
1094 static HRESULT WINAPI
1095 IDirectDrawImpl_FlipToGDISurface(IDirectDraw7 *iface)
1097 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1098 TRACE("(%p)\n", This);
1100 return DD_OK;
1103 /*****************************************************************************
1104 * IDirectDraw7::WaitForVerticalBlank
1106 * This method allows applications to get in sync with the vertical blank
1107 * interval.
1108 * The wormhole demo in the DirectX 7 sdk uses this call, and it doesn't
1109 * redraw the screen, most likely because of this stub
1111 * Parameters:
1112 * Flags: one of DDWAITVB_BLOCKBEGIN, DDWAITVB_BLOCKBEGINEVENT
1113 * or DDWAITVB_BLOCKEND
1114 * h: Not used, according to MSDN
1116 * Returns:
1117 * Always returns DD_OK
1119 *****************************************************************************/
1120 static HRESULT WINAPI
1121 IDirectDrawImpl_WaitForVerticalBlank(IDirectDraw7 *iface,
1122 DWORD Flags,
1123 HANDLE h)
1125 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1126 FIXME("(%p)->(%x,%p): Stub\n", This, Flags, h);
1128 /* MSDN says DDWAITVB_BLOCKBEGINEVENT is not supported */
1129 if(Flags & DDWAITVB_BLOCKBEGINEVENT)
1130 return DDERR_UNSUPPORTED; /* unchecked */
1132 return DD_OK;
1135 /*****************************************************************************
1136 * IDirectDraw7::GetScanLine
1138 * Returns the scan line that is being drawn on the monitor
1140 * Parameters:
1141 * Scanline: Address to write the scan line value to
1143 * Returns:
1144 * Always returns DD_OK
1146 *****************************************************************************/
1147 static HRESULT WINAPI IDirectDrawImpl_GetScanLine(IDirectDraw7 *iface, DWORD *Scanline)
1149 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1150 static BOOL hide = FALSE;
1151 WINED3DDISPLAYMODE Mode;
1153 /* This function is called often, so print the fixme only once */
1154 EnterCriticalSection(&ddraw_cs);
1155 if(!hide)
1157 FIXME("(%p)->(%p): Semi-Stub\n", This, Scanline);
1158 hide = TRUE;
1161 IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1163 &Mode);
1165 /* Fake the line sweeping of the monitor */
1166 /* FIXME: We should synchronize with a source to keep the refresh rate */
1167 *Scanline = This->cur_scanline++;
1168 /* Assume 20 scan lines in the vertical blank */
1169 if (This->cur_scanline >= Mode.Height + 20)
1170 This->cur_scanline = 0;
1172 LeaveCriticalSection(&ddraw_cs);
1173 return DD_OK;
1176 /*****************************************************************************
1177 * IDirectDraw7::TestCooperativeLevel
1179 * Informs the application about the state of the video adapter, depending
1180 * on the cooperative level
1182 * Returns:
1183 * DD_OK if the device is in a sane state
1184 * DDERR_NOEXCLUSIVEMODE or DDERR_EXCLUSIVEMODEALREADYSET
1185 * if the state is not correct(See below)
1187 *****************************************************************************/
1188 static HRESULT WINAPI
1189 IDirectDrawImpl_TestCooperativeLevel(IDirectDraw7 *iface)
1191 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1192 HRESULT hr;
1193 TRACE("(%p)\n", This);
1195 EnterCriticalSection(&ddraw_cs);
1196 /* Description from MSDN:
1197 * For fullscreen apps return DDERR_NOEXCLUSIVEMODE if the user switched
1198 * away from the app with e.g. alt-tab. Windowed apps receive
1199 * DDERR_EXCLUSIVEMODEALREADYSET if another application created a
1200 * DirectDraw object in exclusive mode. DDERR_WRONGMODE is returned,
1201 * when the video mode has changed
1204 hr = IWineD3DDevice_TestCooperativeLevel(This->wineD3DDevice);
1206 /* Fix the result value. These values are mapped from their
1207 * d3d9 counterpart.
1209 switch(hr)
1211 case WINED3DERR_DEVICELOST:
1212 if(This->cooperative_level & DDSCL_EXCLUSIVE)
1214 LeaveCriticalSection(&ddraw_cs);
1215 return DDERR_NOEXCLUSIVEMODE;
1217 else
1219 LeaveCriticalSection(&ddraw_cs);
1220 return DDERR_EXCLUSIVEMODEALREADYSET;
1223 case WINED3DERR_DEVICENOTRESET:
1224 LeaveCriticalSection(&ddraw_cs);
1225 return DD_OK;
1227 case WINED3D_OK:
1228 LeaveCriticalSection(&ddraw_cs);
1229 return DD_OK;
1231 case WINED3DERR_DRIVERINTERNALERROR:
1232 default:
1233 ERR("(%p) Unexpected return value %08x from wineD3D, "
1234 " returning DD_OK\n", This, hr);
1237 LeaveCriticalSection(&ddraw_cs);
1238 return DD_OK;
1241 /*****************************************************************************
1242 * IDirectDraw7::GetGDISurface
1244 * Returns the surface that GDI is treating as the primary surface.
1245 * For Wine this is the front buffer
1247 * Params:
1248 * GDISurface: Address to write the surface pointer to
1250 * Returns:
1251 * DD_OK if the surface was found
1252 * DDERR_NOTFOUND if the GDI surface wasn't found
1254 *****************************************************************************/
1255 static HRESULT WINAPI
1256 IDirectDrawImpl_GetGDISurface(IDirectDraw7 *iface,
1257 IDirectDrawSurface7 **GDISurface)
1259 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1260 IWineD3DSurface *Surf;
1261 IDirectDrawSurface7 *ddsurf;
1262 HRESULT hr;
1263 DDSCAPS2 ddsCaps;
1264 TRACE("(%p)->(%p)\n", This, GDISurface);
1266 /* Get the back buffer from the wineD3DDevice and search its
1267 * attached surfaces for the front buffer
1269 EnterCriticalSection(&ddraw_cs);
1270 hr = IWineD3DDevice_GetBackBuffer(This->wineD3DDevice,
1271 0, /* SwapChain */
1272 0, /* first back buffer*/
1273 WINED3DBACKBUFFER_TYPE_MONO,
1274 &Surf);
1276 if( (hr != D3D_OK) ||
1277 (!Surf) )
1279 ERR("IWineD3DDevice::GetBackBuffer failed\n");
1280 LeaveCriticalSection(&ddraw_cs);
1281 return DDERR_NOTFOUND;
1284 /* GetBackBuffer AddRef()ed the surface, release it */
1285 IWineD3DSurface_Release(Surf);
1287 IWineD3DSurface_GetParent(Surf,
1288 (IUnknown **) &ddsurf);
1289 IDirectDrawSurface7_Release(ddsurf); /* For the GetParent */
1291 /* Find the front buffer */
1292 ddsCaps.dwCaps = DDSCAPS_FRONTBUFFER;
1293 hr = IDirectDrawSurface7_GetAttachedSurface(ddsurf,
1294 &ddsCaps,
1295 GDISurface);
1296 if(hr != DD_OK)
1298 ERR("IDirectDrawSurface7::GetAttachedSurface failed, hr = %x\n", hr);
1301 /* The AddRef is OK this time */
1302 LeaveCriticalSection(&ddraw_cs);
1303 return hr;
1306 /*****************************************************************************
1307 * IDirectDraw7::EnumDisplayModes
1309 * Enumerates the supported Display modes. The modes can be filtered with
1310 * the DDSD parameter.
1312 * Params:
1313 * Flags: can be DDEDM_REFRESHRATES and DDEDM_STANDARDVGAMODES
1314 * DDSD: Surface description to filter the modes
1315 * Context: Pointer passed back to the callback function
1316 * cb: Application-provided callback function
1318 * Returns:
1319 * DD_OK on success
1320 * DDERR_INVALIDPARAMS if the callback wasn't set
1322 *****************************************************************************/
1323 static HRESULT WINAPI
1324 IDirectDrawImpl_EnumDisplayModes(IDirectDraw7 *iface,
1325 DWORD Flags,
1326 DDSURFACEDESC2 *DDSD,
1327 void *Context,
1328 LPDDENUMMODESCALLBACK2 cb)
1330 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1331 unsigned int modenum, fmt;
1332 WINED3DFORMAT pixelformat = WINED3DFMT_UNKNOWN;
1333 WINED3DDISPLAYMODE mode;
1334 DDSURFACEDESC2 callback_sd;
1335 WINED3DDISPLAYMODE *enum_modes = NULL;
1336 unsigned enum_mode_count = 0, enum_mode_array_size = 0;
1338 WINED3DFORMAT checkFormatList[] =
1340 WINED3DFMT_R8G8B8,
1341 WINED3DFMT_A8R8G8B8,
1342 WINED3DFMT_X8R8G8B8,
1343 WINED3DFMT_R5G6B5,
1344 WINED3DFMT_X1R5G5B5,
1345 WINED3DFMT_A1R5G5B5,
1346 WINED3DFMT_A4R4G4B4,
1347 WINED3DFMT_R3G3B2,
1348 WINED3DFMT_A8R3G3B2,
1349 WINED3DFMT_X4R4G4B4,
1350 WINED3DFMT_A2B10G10R10,
1351 WINED3DFMT_A8B8G8R8,
1352 WINED3DFMT_X8B8G8R8,
1353 WINED3DFMT_A2R10G10B10,
1354 WINED3DFMT_A8P8,
1355 WINED3DFMT_P8
1358 TRACE("(%p)->(%p,%p,%p): Relay\n", This, DDSD, Context, cb);
1360 EnterCriticalSection(&ddraw_cs);
1361 /* This looks sane */
1362 if(!cb)
1364 LeaveCriticalSection(&ddraw_cs);
1365 return DDERR_INVALIDPARAMS;
1368 if(DDSD)
1370 if ((DDSD->dwFlags & DDSD_PIXELFORMAT) && (DDSD->u4.ddpfPixelFormat.dwFlags & DDPF_RGB) )
1371 pixelformat = PixelFormat_DD2WineD3D(&DDSD->u4.ddpfPixelFormat);
1374 if(!(Flags & DDEDM_REFRESHRATES))
1376 enum_mode_array_size = 16;
1377 enum_modes = HeapAlloc(GetProcessHeap(), 0, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1378 if (!enum_modes)
1380 ERR("Out of memory\n");
1381 LeaveCriticalSection(&ddraw_cs);
1382 return DDERR_OUTOFMEMORY;
1386 for(fmt = 0; fmt < (sizeof(checkFormatList) / sizeof(checkFormatList[0])); fmt++)
1388 if(pixelformat != WINED3DFMT_UNKNOWN && checkFormatList[fmt] != pixelformat)
1390 continue;
1393 modenum = 0;
1394 while(IWineD3D_EnumAdapterModes(This->wineD3D,
1395 WINED3DADAPTER_DEFAULT,
1396 checkFormatList[fmt],
1397 modenum++,
1398 &mode) == WINED3D_OK)
1400 if(DDSD)
1402 if(DDSD->dwFlags & DDSD_WIDTH && mode.Width != DDSD->dwWidth) continue;
1403 if(DDSD->dwFlags & DDSD_HEIGHT && mode.Height != DDSD->dwHeight) continue;
1406 if(!(Flags & DDEDM_REFRESHRATES))
1408 /* DX docs state EnumDisplayMode should return only unique modes. If DDEDM_REFRESHRATES is not set, refresh
1409 * rate doesn't matter when determining if the mode is unique. So modes only differing in refresh rate have
1410 * to be reduced to a single unique result in such case.
1412 BOOL found = FALSE;
1413 unsigned i;
1415 for (i = 0; i < enum_mode_count; i++)
1417 if(enum_modes[i].Width == mode.Width && enum_modes[i].Height == mode.Height &&
1418 enum_modes[i].Format == mode.Format)
1420 found = TRUE;
1421 break;
1425 if(found) continue;
1428 memset(&callback_sd, 0, sizeof(callback_sd));
1429 callback_sd.dwSize = sizeof(callback_sd);
1430 callback_sd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
1432 callback_sd.dwFlags = DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH;
1433 if(Flags & DDEDM_REFRESHRATES)
1435 callback_sd.dwFlags |= DDSD_REFRESHRATE;
1436 callback_sd.u2.dwRefreshRate = mode.RefreshRate;
1439 callback_sd.dwWidth = mode.Width;
1440 callback_sd.dwHeight = mode.Height;
1442 PixelFormat_WineD3DtoDD(&callback_sd.u4.ddpfPixelFormat, mode.Format);
1444 /* Calc pitch and DWORD align like MSDN says */
1445 callback_sd.u1.lPitch = (callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount / 8) * mode.Width;
1446 callback_sd.u1.lPitch = (callback_sd.u1.lPitch + 3) & ~3;
1448 TRACE("Enumerating %dx%dx%d @%d\n", callback_sd.dwWidth, callback_sd.dwHeight, callback_sd.u4.ddpfPixelFormat.u1.dwRGBBitCount,
1449 callback_sd.u2.dwRefreshRate);
1451 if(cb(&callback_sd, Context) == DDENUMRET_CANCEL)
1453 TRACE("Application asked to terminate the enumeration\n");
1454 HeapFree(GetProcessHeap(), 0, enum_modes);
1455 LeaveCriticalSection(&ddraw_cs);
1456 return DD_OK;
1459 if(!(Flags & DDEDM_REFRESHRATES))
1461 if (enum_mode_count == enum_mode_array_size)
1463 WINED3DDISPLAYMODE *new_enum_modes;
1465 enum_mode_array_size *= 2;
1466 new_enum_modes = HeapReAlloc(GetProcessHeap(), 0, enum_modes, sizeof(WINED3DDISPLAYMODE) * enum_mode_array_size);
1468 if (!new_enum_modes)
1470 ERR("Out of memory\n");
1471 HeapFree(GetProcessHeap(), 0, enum_modes);
1472 LeaveCriticalSection(&ddraw_cs);
1473 return DDERR_OUTOFMEMORY;
1476 enum_modes = new_enum_modes;
1479 enum_modes[enum_mode_count++] = mode;
1484 TRACE("End of enumeration\n");
1485 HeapFree(GetProcessHeap(), 0, enum_modes);
1486 LeaveCriticalSection(&ddraw_cs);
1487 return DD_OK;
1490 /*****************************************************************************
1491 * IDirectDraw7::EvaluateMode
1493 * Used with IDirectDraw7::StartModeTest to test video modes.
1494 * EvaluateMode is used to pass or fail a mode, and continue with the next
1495 * mode
1497 * Params:
1498 * Flags: DDEM_MODEPASSED or DDEM_MODEFAILED
1499 * Timeout: Returns the amount of seconds left before the mode would have
1500 * been failed automatically
1502 * Returns:
1503 * This implementation always DD_OK, because it's a stub
1505 *****************************************************************************/
1506 static HRESULT WINAPI
1507 IDirectDrawImpl_EvaluateMode(IDirectDraw7 *iface,
1508 DWORD Flags,
1509 DWORD *Timeout)
1511 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1512 FIXME("(%p)->(%d,%p): Stub!\n", This, Flags, Timeout);
1514 /* When implementing this, implement it in WineD3D */
1516 return DD_OK;
1519 /*****************************************************************************
1520 * IDirectDraw7::GetDeviceIdentifier
1522 * Returns the device identifier, which gives information about the driver
1523 * Our device identifier is defined at the beginning of this file.
1525 * Params:
1526 * DDDI: Address for the returned structure
1527 * Flags: Can be DDGDI_GETHOSTIDENTIFIER
1529 * Returns:
1530 * On success it returns DD_OK
1531 * DDERR_INVALIDPARAMS if DDDI is NULL
1533 *****************************************************************************/
1534 static HRESULT WINAPI
1535 IDirectDrawImpl_GetDeviceIdentifier(IDirectDraw7 *iface,
1536 DDDEVICEIDENTIFIER2 *DDDI,
1537 DWORD Flags)
1539 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1540 TRACE("(%p)->(%p,%08x)\n", This, DDDI, Flags);
1542 if(!DDDI)
1543 return DDERR_INVALIDPARAMS;
1545 /* The DDGDI_GETHOSTIDENTIFIER returns the information about the 2D
1546 * host adapter, if there's a secondary 3D adapter. This doesn't apply
1547 * to any modern hardware, nor is it interesting for Wine, so ignore it
1550 *DDDI = deviceidentifier;
1551 return DD_OK;
1554 /*****************************************************************************
1555 * IDirectDraw7::GetSurfaceFromDC
1557 * Returns the Surface for a GDI device context handle.
1558 * Is this related to IDirectDrawSurface::GetDC ???
1560 * Params:
1561 * hdc: hdc to return the surface for
1562 * Surface: Address to write the surface pointer to
1564 * Returns:
1565 * Always returns DD_OK because it's a stub
1567 *****************************************************************************/
1568 static HRESULT WINAPI
1569 IDirectDrawImpl_GetSurfaceFromDC(IDirectDraw7 *iface,
1570 HDC hdc,
1571 IDirectDrawSurface7 **Surface)
1573 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1574 FIXME("(%p)->(%p,%p): Stub!\n", This, hdc, Surface);
1576 /* Implementation idea if needed: Loop through all surfaces and compare
1577 * their hdc with hdc. Implement it in WineD3D! */
1578 return DDERR_NOTFOUND;
1581 /*****************************************************************************
1582 * IDirectDraw7::RestoreAllSurfaces
1584 * Calls the restore method of all surfaces
1586 * Params:
1588 * Returns:
1589 * Always returns DD_OK because it's a stub
1591 *****************************************************************************/
1592 static HRESULT WINAPI
1593 IDirectDrawImpl_RestoreAllSurfaces(IDirectDraw7 *iface)
1595 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1596 FIXME("(%p): Stub\n", This);
1598 /* This isn't hard to implement: Enumerate all WineD3D surfaces,
1599 * get their parent and call their restore method. Do not implement
1600 * it in WineD3D, as restoring a surface means re-creating the
1601 * WineD3DDSurface
1603 return DD_OK;
1606 /*****************************************************************************
1607 * IDirectDraw7::StartModeTest
1609 * Tests the specified video modes to update the system registry with
1610 * refresh rate information. StartModeTest starts the mode test,
1611 * EvaluateMode is used to fail or pass a mode. If EvaluateMode
1612 * isn't called within 15 seconds, the mode is failed automatically
1614 * As refresh rates are handled by the X server, I don't think this
1615 * Method is important
1617 * Params:
1618 * Modes: An array of mode specifications
1619 * NumModes: The number of modes in Modes
1620 * Flags: Some flags...
1622 * Returns:
1623 * Returns DDERR_TESTFINISHED if flags contains DDSMT_ISTESTREQUIRED,
1624 * if no modes are passed, DDERR_INVALIDPARAMS is returned,
1625 * otherwise DD_OK
1627 *****************************************************************************/
1628 static HRESULT WINAPI
1629 IDirectDrawImpl_StartModeTest(IDirectDraw7 *iface,
1630 SIZE *Modes,
1631 DWORD NumModes,
1632 DWORD Flags)
1634 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
1635 WARN("(%p)->(%p, %d, %x): Semi-Stub, most likely harmless\n", This, Modes, NumModes, Flags);
1637 /* This looks sane */
1638 if( (!Modes) || (NumModes == 0) ) return DDERR_INVALIDPARAMS;
1640 /* DDSMT_ISTESTREQUIRED asks if a mode test is necessary.
1641 * As it is not, DDERR_TESTFINISHED is returned
1642 * (hopefully that's correct
1644 if(Flags & DDSMT_ISTESTREQUIRED) return DDERR_TESTFINISHED;
1645 * well, that value doesn't (yet) exist in the wine headers, so ignore it
1648 return DD_OK;
1651 /*****************************************************************************
1652 * IDirectDrawImpl_RecreateSurfacesCallback
1654 * Enumeration callback for IDirectDrawImpl_RecreateAllSurfaces.
1655 * It re-recreates the WineD3DSurface. It's pretty straightforward
1657 *****************************************************************************/
1658 HRESULT WINAPI
1659 IDirectDrawImpl_RecreateSurfacesCallback(IDirectDrawSurface7 *surf,
1660 DDSURFACEDESC2 *desc,
1661 void *Context)
1663 IDirectDrawSurfaceImpl *surfImpl = ICOM_OBJECT(IDirectDrawSurfaceImpl,
1664 IDirectDrawSurface7,
1665 surf);
1666 IDirectDrawImpl *This = surfImpl->ddraw;
1667 IUnknown *Parent;
1668 IParentImpl *parImpl = NULL;
1669 IWineD3DSurface *wineD3DSurface;
1670 IWineD3DSwapChain *swapchain;
1671 HRESULT hr;
1672 void *tmp;
1673 IWineD3DClipper *clipper = NULL;
1675 WINED3DSURFACE_DESC Desc;
1676 WINED3DFORMAT Format;
1677 WINED3DRESOURCETYPE Type;
1678 DWORD Usage;
1679 WINED3DPOOL Pool;
1680 UINT Size;
1682 WINED3DMULTISAMPLE_TYPE MultiSampleType;
1683 DWORD MultiSampleQuality;
1684 UINT Width;
1685 UINT Height;
1687 TRACE("(%p): Enumerated Surface %p\n", This, surfImpl);
1689 /* For the enumeration */
1690 IDirectDrawSurface7_Release(surf);
1692 if(surfImpl->ImplType == This->ImplType) return DDENUMRET_OK; /* Continue */
1694 /* Get the objects */
1695 swapchain = surfImpl->wineD3DSwapChain;
1696 surfImpl->wineD3DSwapChain = NULL;
1697 wineD3DSurface = surfImpl->WineD3DSurface;
1698 IWineD3DSurface_GetParent(wineD3DSurface, &Parent);
1699 IUnknown_Release(Parent); /* For the getParent */
1701 /* Is the parent an IParent interface? */
1702 if(IUnknown_QueryInterface(Parent, &IID_IParent, &tmp) == S_OK)
1704 /* It is a IParent interface! */
1705 IUnknown_Release(Parent); /* For the QueryInterface */
1706 parImpl = ICOM_OBJECT(IParentImpl, IParent, Parent);
1707 /* Release the reference the parent interface is holding */
1708 IWineD3DSurface_Release(wineD3DSurface);
1711 /* get the clipper */
1712 IWineD3DSurface_GetClipper(wineD3DSurface, &clipper);
1714 /* Get the surface properties */
1715 Desc.Format = &Format;
1716 Desc.Type = &Type;
1717 Desc.Usage = &Usage;
1718 Desc.Pool = &Pool;
1719 Desc.Size = &Size;
1720 Desc.MultiSampleType = &MultiSampleType;
1721 Desc.MultiSampleQuality = &MultiSampleQuality;
1722 Desc.Width = &Width;
1723 Desc.Height = &Height;
1725 hr = IWineD3DSurface_GetDesc(wineD3DSurface, &Desc);
1726 if(hr != D3D_OK) return hr;
1728 if(swapchain) {
1729 /* If there's a swapchain, it owns the IParent interface. Create a new one for the
1730 * new surface
1732 parImpl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*parImpl));
1733 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
1734 parImpl->ref = 1;
1736 Parent = (IUnknown *) parImpl;
1739 /* Create the new surface */
1740 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
1741 Width, Height, Format,
1742 TRUE /* Lockable */,
1743 FALSE /* Discard */,
1744 surfImpl->mipmap_level,
1745 &surfImpl->WineD3DSurface,
1746 Type,
1747 Usage,
1748 Pool,
1749 MultiSampleType,
1750 MultiSampleQuality,
1751 0 /* SharedHandle */,
1752 This->ImplType,
1753 Parent);
1755 if(hr != D3D_OK)
1756 return hr;
1758 IWineD3DSurface_SetClipper(surfImpl->WineD3DSurface, clipper);
1760 /* Update the IParent if it exists */
1761 if(parImpl)
1763 parImpl->child = (IUnknown *) surfImpl->WineD3DSurface;
1764 /* Add a reference for the IParent */
1765 IWineD3DSurface_AddRef(surfImpl->WineD3DSurface);
1767 /* TODO: Copy the surface content, except for render targets */
1769 /* If there's a swapchain, it owns the wined3d surfaces. So Destroy
1770 * the swapchain
1772 if(swapchain) {
1773 /* The backbuffers have the swapchain set as well, but the primary
1774 * owns it and destroys it
1776 if(surfImpl->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
1777 IWineD3DDevice_UninitGDI(This->wineD3DDevice, D3D7CB_DestroySwapChain);
1779 surfImpl->isRenderTarget = FALSE;
1780 } else {
1781 if(IWineD3DSurface_Release(wineD3DSurface) == 0)
1782 TRACE("Surface released successful, next surface\n");
1783 else
1784 ERR("Something's still holding the old WineD3DSurface\n");
1787 surfImpl->ImplType = This->ImplType;
1789 if(clipper)
1791 IWineD3DClipper_Release(clipper);
1793 return DDENUMRET_OK;
1796 /*****************************************************************************
1797 * IDirectDrawImpl_RecreateAllSurfaces
1799 * A function, that converts all wineD3DSurfaces to the new implementation type
1800 * It enumerates all surfaces with IWineD3DDevice::EnumSurfaces, creates a
1801 * new WineD3DSurface, copies the content and releases the old surface
1803 *****************************************************************************/
1804 static HRESULT
1805 IDirectDrawImpl_RecreateAllSurfaces(IDirectDrawImpl *This)
1807 DDSURFACEDESC2 desc;
1808 TRACE("(%p): Switch to implementation %d\n", This, This->ImplType);
1810 if(This->ImplType != SURFACE_OPENGL && This->d3d_initialized)
1812 /* Should happen almost never */
1813 FIXME("(%p) Switching to non-opengl surfaces with d3d started. Is this a bug?\n", This);
1814 /* Shutdown d3d */
1815 IWineD3DDevice_Uninit3D(This->wineD3DDevice, D3D7CB_DestroyDepthStencilSurface, D3D7CB_DestroySwapChain);
1817 /* Contrary: D3D starting is handled by the caller, because it knows the render target */
1819 memset(&desc, 0, sizeof(desc));
1820 desc.dwSize = sizeof(desc);
1822 return IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(This, IDirectDraw7),
1824 &desc,
1825 This,
1826 IDirectDrawImpl_RecreateSurfacesCallback);
1829 /*****************************************************************************
1830 * D3D7CB_CreateSurface
1832 * Callback function for IDirect3DDevice_CreateTexture. It searches for the
1833 * correct mipmap sublevel, and returns it to WineD3D.
1834 * The surfaces are created already by IDirectDraw7::CreateSurface
1836 * Params:
1837 * With, Height: With and height of the surface
1838 * Format: The requested format
1839 * Usage, Pool: D3DUSAGE and D3DPOOL of the surface
1840 * level: The mipmap level
1841 * Face: The cube map face type
1842 * Surface: Pointer to pass the created surface back at
1843 * SharedHandle: NULL
1845 * Returns:
1846 * D3D_OK
1848 *****************************************************************************/
1849 static HRESULT WINAPI
1850 D3D7CB_CreateSurface(IUnknown *device,
1851 IUnknown *pSuperior,
1852 UINT Width, UINT Height,
1853 WINED3DFORMAT Format,
1854 DWORD Usage, WINED3DPOOL Pool, UINT level,
1855 WINED3DCUBEMAP_FACES Face,
1856 IWineD3DSurface **Surface,
1857 HANDLE *SharedHandle)
1859 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
1860 IDirectDrawSurfaceImpl *surf = NULL;
1861 int i = 0;
1862 DDSCAPS2 searchcaps = This->tex_root->surface_desc.ddsCaps;
1863 TRACE("(%p) call back. surf=%p. Face %d level %d\n", device, This->tex_root, Face, level);
1865 searchcaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
1866 switch(Face)
1868 case WINED3DCUBEMAP_FACE_POSITIVE_X:
1869 TRACE("Asked for positive x\n");
1870 if(searchcaps.dwCaps2 & DDSCAPS2_CUBEMAP) {
1871 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
1873 surf = This->tex_root; break;
1874 case WINED3DCUBEMAP_FACE_NEGATIVE_X:
1875 TRACE("Asked for negative x\n");
1876 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX; break;
1877 case WINED3DCUBEMAP_FACE_POSITIVE_Y:
1878 TRACE("Asked for positive y\n");
1879 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY; break;
1880 case WINED3DCUBEMAP_FACE_NEGATIVE_Y:
1881 TRACE("Asked for negative y\n");
1882 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY; break;
1883 case WINED3DCUBEMAP_FACE_POSITIVE_Z:
1884 TRACE("Asked for positive z\n");
1885 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ; break;
1886 case WINED3DCUBEMAP_FACE_NEGATIVE_Z:
1887 TRACE("Asked for negative z\n");
1888 searchcaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ; break;
1889 default: {ERR("Unexpected cube face\n");} /* Stupid compiler */
1892 if(!surf)
1894 IDirectDrawSurface7 *attached;
1895 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This->tex_root, IDirectDrawSurface7),
1896 &searchcaps,
1897 &attached);
1898 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1899 IDirectDrawSurface7_Release(attached);
1901 if(!surf) ERR("root search surface not found\n");
1903 /* Find the wanted mipmap. There are enough mipmaps in the chain */
1904 while(i < level)
1906 IDirectDrawSurface7 *attached;
1907 IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(surf, IDirectDrawSurface7),
1908 &searchcaps,
1909 &attached);
1910 if(!attached) ERR("Surface not found\n");
1911 surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, attached);
1912 IDirectDrawSurface7_Release(attached);
1913 i++;
1916 /* Return the surface */
1917 *Surface = surf->WineD3DSurface;
1919 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *Surface, surf);
1920 return D3D_OK;
1923 ULONG WINAPI D3D7CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
1924 IUnknown* swapChainParent;
1925 TRACE("(%p) call back\n", pSwapChain);
1927 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
1928 IUnknown_Release(swapChainParent);
1929 return IUnknown_Release(swapChainParent);
1932 ULONG WINAPI D3D7CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
1933 IUnknown* surfaceParent;
1934 TRACE("(%p) call back\n", pSurface);
1936 IWineD3DSurface_GetParent(pSurface, &surfaceParent);
1937 IUnknown_Release(surfaceParent);
1938 return IUnknown_Release(surfaceParent);
1941 /*****************************************************************************
1942 * IDirectDrawImpl_CreateNewSurface
1944 * A helper function for IDirectDraw7::CreateSurface. It creates a new surface
1945 * with the passed parameters.
1947 * Params:
1948 * DDSD: Description of the surface to create
1949 * Surf: Address to store the interface pointer at
1951 * Returns:
1952 * DD_OK on success
1954 *****************************************************************************/
1955 static HRESULT WINAPI
1956 IDirectDrawImpl_CreateNewSurface(IDirectDrawImpl *This,
1957 DDSURFACEDESC2 *pDDSD,
1958 IDirectDrawSurfaceImpl **ppSurf,
1959 UINT level)
1961 HRESULT hr;
1962 UINT Width = 0, Height = 0;
1963 WINED3DFORMAT Format = WINED3DFMT_UNKNOWN;
1964 WINED3DRESOURCETYPE ResType = WINED3DRTYPE_SURFACE;
1965 DWORD Usage = 0;
1966 WINED3DSURFTYPE ImplType = This->ImplType;
1967 WINED3DSURFACE_DESC Desc;
1968 IUnknown *Parent;
1969 IParentImpl *parImpl = NULL;
1970 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
1972 /* Dummies for GetDesc */
1973 WINED3DPOOL dummy_d3dpool;
1974 WINED3DMULTISAMPLE_TYPE dummy_mst;
1975 UINT dummy_uint;
1976 DWORD dummy_dword;
1978 if (TRACE_ON(ddraw))
1980 TRACE(" (%p) Requesting surface desc :\n", This);
1981 DDRAW_dump_surface_desc(pDDSD);
1984 /* Select the surface type, if it wasn't choosen yet */
1985 if(ImplType == SURFACE_UNKNOWN)
1987 /* Use GL Surfaces if a D3DDEVICE Surface is requested */
1988 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
1990 TRACE("(%p) Choosing GL surfaces because a 3DDEVICE Surface was requested\n", This);
1991 ImplType = SURFACE_OPENGL;
1994 /* Otherwise use GDI surfaces for now */
1995 else
1997 TRACE("(%p) Choosing GDI surfaces for 2D rendering\n", This);
1998 ImplType = SURFACE_GDI;
2001 /* Policy if all surface implementations are available:
2002 * First, check if a default type was set with winecfg. If not,
2003 * try Xrender surfaces, and use them if they work. Next, check if
2004 * accelerated OpenGL is available, and use GL surfaces in this
2005 * case. If all else fails, use GDI surfaces. If a 3DDEVICE surface
2006 * was created, always use OpenGL surfaces.
2008 * (Note: Xrender surfaces are not implemented for now, the
2009 * unaccelerated implementation uses GDI to render in Software)
2012 /* Store the type. If it needs to be changed, all WineD3DSurfaces have to
2013 * be re-created. This could be done with IDirectDrawSurface7::Restore
2015 This->ImplType = ImplType;
2017 else
2019 if((pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE ) &&
2020 (This->ImplType != SURFACE_OPENGL ) && DefaultSurfaceType == SURFACE_UNKNOWN)
2022 /* We have to change to OpenGL,
2023 * and re-create all WineD3DSurfaces
2025 ImplType = SURFACE_OPENGL;
2026 This->ImplType = ImplType;
2027 TRACE("(%p) Re-creating all surfaces\n", This);
2028 IDirectDrawImpl_RecreateAllSurfaces(This);
2029 TRACE("(%p) Done recreating all surfaces\n", This);
2031 else if(This->ImplType != SURFACE_OPENGL && pDDSD->ddsCaps.dwCaps & DDSCAPS_3DDEVICE)
2033 WARN("The application requests a 3D capable surface, but a non-opengl surface was set in the registry\n");
2034 /* Do not fail surface creation, only fail 3D device creation */
2038 if (!(pDDSD->ddsCaps.dwCaps & (DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY)) &&
2039 !((pDDSD->ddsCaps.dwCaps & DDSCAPS_TEXTURE) && (pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)) )
2041 /* Tests show surfaces without memory flags get these flags added right after creation. */
2042 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
2044 /* Get the correct wined3d usage */
2045 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE |
2046 DDSCAPS_3DDEVICE ) )
2048 Usage |= WINED3DUSAGE_RENDERTARGET;
2050 pDDSD->ddsCaps.dwCaps |= DDSCAPS_VISIBLE;
2052 if (pDDSD->ddsCaps.dwCaps & (DDSCAPS_OVERLAY))
2054 Usage |= WINED3DUSAGE_OVERLAY;
2056 if(This->depthstencil || (pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER) )
2058 /* The depth stencil creation callback sets this flag.
2059 * Set the WineD3D usage to let it know that it's a depth
2060 * Stencil surface.
2062 Usage |= WINED3DUSAGE_DEPTHSTENCIL;
2064 if(pDDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2066 Pool = WINED3DPOOL_SYSTEMMEM;
2068 else if(pDDSD->ddsCaps.dwCaps2 & DDSCAPS2_TEXTUREMANAGE)
2070 Pool = WINED3DPOOL_MANAGED;
2071 /* Managed textures have the system memory flag set */
2072 pDDSD->ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
2074 else if(pDDSD->ddsCaps.dwCaps & DDSCAPS_VIDEOMEMORY)
2076 /* Videomemory adds localvidmem, this is mutually exclusive with systemmemory
2077 * and texturemanage
2079 pDDSD->ddsCaps.dwCaps |= DDSCAPS_LOCALVIDMEM;
2082 Format = PixelFormat_DD2WineD3D(&pDDSD->u4.ddpfPixelFormat);
2083 if(Format == WINED3DFMT_UNKNOWN)
2085 ERR("Unsupported / Unknown pixelformat\n");
2086 return DDERR_INVALIDPIXELFORMAT;
2089 /* Create the Surface object */
2090 *ppSurf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawSurfaceImpl));
2091 if(!*ppSurf)
2093 ERR("(%p) Error allocating memory for a surface\n", This);
2094 return DDERR_OUTOFVIDEOMEMORY;
2096 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface7, IDirectDrawSurface7_Vtbl);
2097 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawSurface3, IDirectDrawSurface3_Vtbl);
2098 ICOM_INIT_INTERFACE(*ppSurf, IDirectDrawGammaControl, IDirectDrawGammaControl_Vtbl);
2099 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture2, IDirect3DTexture2_Vtbl);
2100 ICOM_INIT_INTERFACE(*ppSurf, IDirect3DTexture, IDirect3DTexture1_Vtbl);
2101 (*ppSurf)->ref = 1;
2102 (*ppSurf)->version = 7;
2103 (*ppSurf)->ddraw = This;
2104 (*ppSurf)->surface_desc.dwSize = sizeof(DDSURFACEDESC2);
2105 (*ppSurf)->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
2106 DD_STRUCT_COPY_BYSIZE(&(*ppSurf)->surface_desc, pDDSD);
2108 /* Surface attachments */
2109 (*ppSurf)->next_attached = NULL;
2110 (*ppSurf)->first_attached = *ppSurf;
2112 /* Needed to re-create the surface on an implementation change */
2113 (*ppSurf)->ImplType = ImplType;
2115 /* For D3DDevice creation */
2116 (*ppSurf)->isRenderTarget = FALSE;
2118 /* A trace message for debugging */
2119 TRACE("(%p) Created IDirectDrawSurface implementation structure at %p\n", This, *ppSurf);
2121 if(pDDSD->ddsCaps.dwCaps & ( DDSCAPS_PRIMARYSURFACE | DDSCAPS_TEXTURE | DDSCAPS_3DDEVICE) )
2123 /* Render targets and textures need a IParent interface,
2124 * because WineD3D will destroy them when the swapchain
2125 * is released
2127 parImpl = HeapAlloc(GetProcessHeap(), 0, sizeof(IParentImpl));
2128 if(!parImpl)
2130 ERR("Out of memory when allocating memory for a IParent implementation\n");
2131 return DDERR_OUTOFMEMORY;
2133 parImpl->ref = 1;
2134 ICOM_INIT_INTERFACE(parImpl, IParent, IParent_Vtbl);
2135 Parent = (IUnknown *) ICOM_INTERFACE(parImpl, IParent);
2136 TRACE("Using IParent interface %p as parent\n", parImpl);
2138 else
2140 /* Use the surface as parent */
2141 Parent = (IUnknown *) ICOM_INTERFACE(*ppSurf, IDirectDrawSurface7);
2142 TRACE("Using Surface interface %p as parent\n", *ppSurf);
2145 /* Now create the WineD3D Surface */
2146 hr = IWineD3DDevice_CreateSurface(This->wineD3DDevice,
2147 pDDSD->dwWidth,
2148 pDDSD->dwHeight,
2149 Format,
2150 TRUE /* Lockable */,
2151 FALSE /* Discard */,
2152 level,
2153 &(*ppSurf)->WineD3DSurface,
2154 ResType, Usage,
2155 Pool,
2156 WINED3DMULTISAMPLE_NONE,
2157 0 /* MultiSampleQuality */,
2158 0 /* SharedHandle */,
2159 ImplType,
2160 Parent);
2162 if(hr != D3D_OK)
2164 ERR("IWineD3DDevice::CreateSurface failed. hr = %08x\n", hr);
2165 return hr;
2168 /* Set the child of the parent implementation if it exists */
2169 if(parImpl)
2171 parImpl->child = (IUnknown *) (*ppSurf)->WineD3DSurface;
2172 /* The IParent releases the WineD3DSurface, and
2173 * the ddraw surface does that too. Hold a reference
2175 IWineD3DSurface_AddRef((*ppSurf)->WineD3DSurface);
2178 /* Increase the surface counter, and attach the surface */
2179 InterlockedIncrement(&This->surfaces);
2180 list_add_head(&This->surface_list, &(*ppSurf)->surface_list_entry);
2182 /* Here we could store all created surfaces in the DirectDrawImpl structure,
2183 * But this could also be delegated to WineDDraw, as it keeps track of all its
2184 * resources. Not implemented for now, as there are more important things ;)
2187 /* Get the pixel format of the WineD3DSurface and store it.
2188 * Don't use the Format choosen above, WineD3D might have
2189 * changed it
2191 Desc.Format = &Format;
2192 Desc.Type = &ResType;
2193 Desc.Usage = &Usage;
2194 Desc.Pool = &dummy_d3dpool;
2195 Desc.Size = &dummy_uint;
2196 Desc.MultiSampleType = &dummy_mst;
2197 Desc.MultiSampleQuality = &dummy_dword;
2198 Desc.Width = &Width;
2199 Desc.Height = &Height;
2201 (*ppSurf)->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
2202 hr = IWineD3DSurface_GetDesc((*ppSurf)->WineD3DSurface, &Desc);
2203 if(hr != D3D_OK)
2205 ERR("IWineD3DSurface::GetDesc failed\n");
2206 IDirectDrawSurface7_Release( (IDirectDrawSurface7 *) *ppSurf);
2207 return hr;
2210 if(Format == WINED3DFMT_UNKNOWN)
2212 FIXME("IWineD3DSurface::GetDesc returned WINED3DFMT_UNKNOWN\n");
2214 PixelFormat_WineD3DtoDD( &(*ppSurf)->surface_desc.u4.ddpfPixelFormat, Format);
2216 /* Anno 1602 stores the pitch right after surface creation, so make sure it's there.
2217 * I can't LockRect() the surface here because if OpenGL surfaces are in use, the
2218 * WineD3DDevice might not be usable for 3D yet, so an extra method was created.
2219 * TODO: Test other fourcc formats
2221 if(Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3 ||
2222 Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5)
2224 (*ppSurf)->surface_desc.dwFlags |= DDSD_LINEARSIZE;
2225 if(Format == WINED3DFMT_DXT1)
2227 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height) / 2;
2229 else
2231 (*ppSurf)->surface_desc.u1.dwLinearSize = max(4, Width) * max(4, Height);
2234 else
2236 (*ppSurf)->surface_desc.dwFlags |= DDSD_PITCH;
2237 (*ppSurf)->surface_desc.u1.lPitch = IWineD3DSurface_GetPitch((*ppSurf)->WineD3DSurface);
2240 /* Application passed a color key? Set it! */
2241 if(pDDSD->dwFlags & DDSD_CKDESTOVERLAY)
2243 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2244 DDCKEY_DESTOVERLAY,
2245 (WINEDDCOLORKEY *) &pDDSD->u3.ddckCKDestOverlay);
2247 if(pDDSD->dwFlags & DDSD_CKDESTBLT)
2249 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2250 DDCKEY_DESTBLT,
2251 (WINEDDCOLORKEY *) &pDDSD->ddckCKDestBlt);
2253 if(pDDSD->dwFlags & DDSD_CKSRCOVERLAY)
2255 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2256 DDCKEY_SRCOVERLAY,
2257 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcOverlay);
2259 if(pDDSD->dwFlags & DDSD_CKSRCBLT)
2261 IWineD3DSurface_SetColorKey((*ppSurf)->WineD3DSurface,
2262 DDCKEY_SRCBLT,
2263 (WINEDDCOLORKEY *) &pDDSD->ddckCKSrcBlt);
2265 if ( pDDSD->dwFlags & DDSD_LPSURFACE)
2267 hr = IWineD3DSurface_SetMem((*ppSurf)->WineD3DSurface, pDDSD->lpSurface);
2268 if(hr != WINED3D_OK)
2270 /* No need for a trace here, wined3d does that for us */
2271 IDirectDrawSurface7_Release(ICOM_INTERFACE((*ppSurf), IDirectDrawSurface7));
2272 return hr;
2276 return DD_OK;
2278 /*****************************************************************************
2279 * CreateAdditionalSurfaces
2281 * Creates a new mipmap chain.
2283 * Params:
2284 * root: Root surface to attach the newly created chain to
2285 * count: number of surfaces to create
2286 * DDSD: Description of the surface. Intentionally not a pointer to avoid side
2287 * effects on the caller
2288 * CubeFaceRoot: Whether the new surface is a root of a cube map face. This
2289 * creates an additional surface without the mipmapping flags
2291 *****************************************************************************/
2292 static HRESULT
2293 CreateAdditionalSurfaces(IDirectDrawImpl *This,
2294 IDirectDrawSurfaceImpl *root,
2295 UINT count,
2296 DDSURFACEDESC2 DDSD,
2297 BOOL CubeFaceRoot)
2299 UINT i, j, level = 0;
2300 HRESULT hr;
2301 IDirectDrawSurfaceImpl *last = root;
2303 for(i = 0; i < count; i++)
2305 IDirectDrawSurfaceImpl *object2 = NULL;
2307 /* increase the mipmap level, but only if a mipmap is created
2308 * In this case, also halve the size
2310 if(DDSD.ddsCaps.dwCaps & DDSCAPS_MIPMAP && !CubeFaceRoot)
2312 level++;
2313 if(DDSD.dwWidth > 1) DDSD.dwWidth /= 2;
2314 if(DDSD.dwHeight > 1) DDSD.dwHeight /= 2;
2315 /* Set the mipmap sublevel flag according to msdn */
2316 DDSD.ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL;
2318 else
2320 DDSD.ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2322 CubeFaceRoot = FALSE;
2324 hr = IDirectDrawImpl_CreateNewSurface(This,
2325 &DDSD,
2326 &object2,
2327 level);
2328 if(hr != DD_OK)
2330 return hr;
2333 /* Add the new surface to the complex attachment array */
2334 for(j = 0; j < MAX_COMPLEX_ATTACHED; j++)
2336 if(last->complex_array[j]) continue;
2337 last->complex_array[j] = object2;
2338 break;
2340 last = object2;
2342 /* Remove the (possible) back buffer cap from the new surface description,
2343 * because only one surface in the flipping chain is a back buffer, one
2344 * is a front buffer, the others are just primary surfaces.
2346 DDSD.ddsCaps.dwCaps &= ~DDSCAPS_BACKBUFFER;
2348 return DD_OK;
2351 /*****************************************************************************
2352 * IDirectDraw7::CreateSurface
2354 * Creates a new IDirectDrawSurface object and returns its interface.
2356 * The surface connections with wined3d are a bit tricky. Basically it works
2357 * like this:
2359 * |------------------------| |-----------------|
2360 * | DDraw surface | | WineD3DSurface |
2361 * | | | |
2362 * | WineD3DSurface |-------------->| |
2363 * | Child |<------------->| Parent |
2364 * |------------------------| |-----------------|
2366 * The DDraw surface is the parent of the wined3d surface, and it releases
2367 * the WineD3DSurface when the ddraw surface is destroyed.
2369 * However, for all surfaces which can be in a container in WineD3D,
2370 * we have to do this. These surfaces are usually complex surfaces,
2371 * so this concerns primary surfaces with a front and a back buffer,
2372 * and textures.
2374 * |------------------------| |-----------------|
2375 * | DDraw surface | | Container |
2376 * | | | |
2377 * | Child |<------------->| Parent |
2378 * | Texture |<------------->| |
2379 * | WineD3DSurface |<----| | Levels |<--|
2380 * | Complex connection | | | | |
2381 * |------------------------| | |-----------------| |
2382 * ^ | |
2383 * | | |
2384 * | | |
2385 * | |------------------| | |-----------------| |
2386 * | | IParent | |-------->| WineD3DSurface | |
2387 * | | | | | |
2388 * | | Child |<------------->| Parent | |
2389 * | | | | Container |<--|
2390 * | |------------------| |-----------------| |
2391 * | |
2392 * | |----------------------| |
2393 * | | DDraw surface 2 | |
2394 * | | | |
2395 * |<->| Complex root Child | |
2396 * | | Texture | |
2397 * | | WineD3DSurface |<----| |
2398 * | |----------------------| | |
2399 * | | |
2400 * | |---------------------| | |-----------------| |
2401 * | | IParent | |----->| WineD3DSurface | |
2402 * | | | | | |
2403 * | | Child |<---------->| Parent | |
2404 * | |---------------------| | Container |<--|
2405 * | |-----------------| |
2406 * | |
2407 * | ---More surfaces can follow--- |
2409 * The reason is that the IWineD3DSwapchain(render target container)
2410 * and the IWineD3DTexure(Texture container) release the parents
2411 * of their surface's children, but by releasing the complex root
2412 * the surfaces which are complexly attached to it are destroyed
2413 * too. See IDirectDrawSurface::Release for a more detailed
2414 * explanation.
2416 * Params:
2417 * DDSD: Description of the surface to create
2418 * Surf: Address to store the interface pointer at
2419 * UnkOuter: Basically for aggregation support, but ddraw doesn't support
2420 * aggregation, so it has to be NULL
2422 * Returns:
2423 * DD_OK on success
2424 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
2425 * DDERR_* if an error occurs
2427 *****************************************************************************/
2428 static HRESULT WINAPI
2429 IDirectDrawImpl_CreateSurface(IDirectDraw7 *iface,
2430 DDSURFACEDESC2 *DDSD,
2431 IDirectDrawSurface7 **Surf,
2432 IUnknown *UnkOuter)
2434 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
2435 IDirectDrawSurfaceImpl *object = NULL;
2436 HRESULT hr;
2437 LONG extra_surfaces = 0;
2438 DDSURFACEDESC2 desc2;
2439 WINED3DDISPLAYMODE Mode;
2441 TRACE("(%p)->(%p,%p,%p)\n", This, DDSD, Surf, UnkOuter);
2443 /* Some checks before we start */
2444 if (TRACE_ON(ddraw))
2446 TRACE(" (%p) Requesting surface desc :\n", This);
2447 DDRAW_dump_surface_desc(DDSD);
2449 EnterCriticalSection(&ddraw_cs);
2451 if (UnkOuter != NULL)
2453 FIXME("(%p) : outer != NULL?\n", This);
2454 LeaveCriticalSection(&ddraw_cs);
2455 return CLASS_E_NOAGGREGATION; /* unchecked */
2458 if (Surf == NULL)
2460 FIXME("(%p) You want to get back a surface? Don't give NULL ptrs!\n", This);
2461 LeaveCriticalSection(&ddraw_cs);
2462 return E_POINTER; /* unchecked */
2465 if (!(DDSD->dwFlags & DDSD_CAPS))
2467 /* DVIDEO.DLL does forget the DDSD_CAPS flag ... *sigh* */
2468 DDSD->dwFlags |= DDSD_CAPS;
2471 if (DDSD->ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD)
2473 /* If the surface is of the 'alloconload' type, ignore the LPSURFACE field */
2474 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2477 if ((DDSD->dwFlags & DDSD_LPSURFACE) && (DDSD->lpSurface == NULL))
2479 /* Frank Herbert's Dune specifies a null pointer for the surface, ignore the LPSURFACE field */
2480 WARN("(%p) Null surface pointer specified, ignore it!\n", This);
2481 DDSD->dwFlags &= ~DDSD_LPSURFACE;
2484 if((DDSD->ddsCaps.dwCaps & (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE)) == (DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE) &&
2485 !(This->cooperative_level & DDSCL_EXCLUSIVE))
2487 TRACE("(%p): Attempt to create a flipable primary surface without DDSCL_EXCLUSIVE set\n", This);
2488 *Surf = NULL;
2489 LeaveCriticalSection(&ddraw_cs);
2490 return DDERR_NOEXCLUSIVEMODE;
2493 if(DDSD->ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER)) {
2494 WARN("Application tried to create an explicit front or back buffer\n");
2495 LeaveCriticalSection(&ddraw_cs);
2496 return DDERR_INVALIDCAPS;
2498 /* Check cube maps but only if the size includes them */
2499 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2501 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES &&
2502 !(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP))
2504 WARN("Cube map faces requested without cube map flag\n");
2505 LeaveCriticalSection(&ddraw_cs);
2506 return DDERR_INVALIDCAPS;
2508 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2509 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) == 0)
2511 WARN("Cube map without faces requested\n");
2512 LeaveCriticalSection(&ddraw_cs);
2513 return DDERR_INVALIDPARAMS;
2516 /* Quick tests confirm those can be created, but we don't do that yet */
2517 if(DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP &&
2518 (DDSD->ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP_ALLFACES) != DDSCAPS2_CUBEMAP_ALLFACES)
2520 FIXME("Partial cube maps not supported yet\n");
2524 /* According to the msdn this flag is ignored by CreateSurface */
2525 if (DDSD->dwSize >= sizeof(DDSURFACEDESC2))
2526 DDSD->ddsCaps.dwCaps2 &= ~DDSCAPS2_MIPMAPSUBLEVEL;
2528 /* Modify some flags */
2529 memset(&desc2, 0, sizeof(desc2));
2530 desc2.dwSize = sizeof(desc2); /* For the struct copy */
2531 DD_STRUCT_COPY_BYSIZE(&desc2, DDSD);
2532 desc2.dwSize = sizeof(desc2); /* To override a possibly smaller size */
2533 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT); /* Just to be sure */
2535 /* Get the video mode from WineD3D - we will need it */
2536 hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
2537 0, /* Swapchain 0 */
2538 &Mode);
2539 if(FAILED(hr))
2541 ERR("Failed to read display mode from wined3d\n");
2542 switch(This->orig_bpp)
2544 case 8:
2545 Mode.Format = WINED3DFMT_P8;
2546 break;
2548 case 15:
2549 Mode.Format = WINED3DFMT_X1R5G5B5;
2550 break;
2552 case 16:
2553 Mode.Format = WINED3DFMT_R5G6B5;
2554 break;
2556 case 24:
2557 Mode.Format = WINED3DFMT_R8G8B8;
2558 break;
2560 case 32:
2561 Mode.Format = WINED3DFMT_X8R8G8B8;
2562 break;
2564 Mode.Width = This->orig_width;
2565 Mode.Height = This->orig_height;
2568 /* No pixelformat given? Use the current screen format */
2569 if(!(desc2.dwFlags & DDSD_PIXELFORMAT))
2571 desc2.dwFlags |= DDSD_PIXELFORMAT;
2572 desc2.u4.ddpfPixelFormat.dwSize=sizeof(DDPIXELFORMAT);
2574 /* Wait: It could be a Z buffer */
2575 if(desc2.ddsCaps.dwCaps & DDSCAPS_ZBUFFER)
2577 switch(desc2.u2.dwMipMapCount) /* Who had this glorious idea? */
2579 case 15:
2580 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D15S1);
2581 break;
2582 case 16:
2583 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D16);
2584 break;
2585 case 24:
2586 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D24X8);
2587 break;
2588 case 32:
2589 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, WINED3DFMT_D32);
2590 break;
2591 default:
2592 ERR("Unknown Z buffer bit depth\n");
2595 else
2597 PixelFormat_WineD3DtoDD(&desc2.u4.ddpfPixelFormat, Mode.Format);
2601 /* No Width or no Height? Use the original screen size
2603 if(!(desc2.dwFlags & DDSD_WIDTH) ||
2604 !(desc2.dwFlags & DDSD_HEIGHT) )
2606 /* Invalid for non-render targets */
2607 if(!(desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
2609 WARN("Creating a non-Primary surface without Width or Height info, returning DDERR_INVALIDPARAMS\n");
2610 *Surf = NULL;
2611 LeaveCriticalSection(&ddraw_cs);
2612 return DDERR_INVALIDPARAMS;
2615 desc2.dwFlags |= DDSD_WIDTH | DDSD_HEIGHT;
2616 desc2.dwWidth = Mode.Width;
2617 desc2.dwHeight = Mode.Height;
2620 /* Mipmap count fixes */
2621 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2623 if(desc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX)
2625 if(desc2.dwFlags & DDSD_MIPMAPCOUNT)
2627 /* Mipmap count is given, should not be 0 */
2628 if( desc2.u2.dwMipMapCount == 0 )
2630 LeaveCriticalSection(&ddraw_cs);
2631 return DDERR_INVALIDPARAMS;
2634 else
2636 /* Undocumented feature: Create sublevels until
2637 * either the width or the height is 1
2639 DWORD min = desc2.dwWidth < desc2.dwHeight ?
2640 desc2.dwWidth : desc2.dwHeight;
2641 desc2.u2.dwMipMapCount = 0;
2642 while( min )
2644 desc2.u2.dwMipMapCount += 1;
2645 min >>= 1;
2649 else
2651 /* Not-complex mipmap -> Mipmapcount = 1 */
2652 desc2.u2.dwMipMapCount = 1;
2654 extra_surfaces = desc2.u2.dwMipMapCount - 1;
2656 /* There's a mipmap count in the created surface in any case */
2657 desc2.dwFlags |= DDSD_MIPMAPCOUNT;
2659 /* If no mipmap is given, the texture has only one level */
2661 /* The first surface is a front buffer, the back buffer is created afterwards */
2662 if( (desc2.dwFlags & DDSD_CAPS) && (desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) )
2664 desc2.ddsCaps.dwCaps |= DDSCAPS_FRONTBUFFER;
2667 /* The root surface in a cube map is positive x */
2668 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2670 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2671 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2674 /* Create the first surface */
2675 hr = IDirectDrawImpl_CreateNewSurface(This, &desc2, &object, 0);
2676 if( hr != DD_OK)
2678 ERR("IDirectDrawImpl_CreateNewSurface failed with %08x\n", hr);
2679 LeaveCriticalSection(&ddraw_cs);
2680 return hr;
2682 object->is_complex_root = TRUE;
2684 *Surf = ICOM_INTERFACE(object, IDirectDrawSurface7);
2686 /* Create Additional surfaces if necessary
2687 * This applies to Primary surfaces which have a back buffer count
2688 * set, but not to mipmap textures. In case of Mipmap textures,
2689 * wineD3D takes care of the creation of additional surfaces
2691 if(DDSD->dwFlags & DDSD_BACKBUFFERCOUNT)
2693 extra_surfaces = DDSD->dwBackBufferCount;
2694 desc2.ddsCaps.dwCaps &= ~DDSCAPS_FRONTBUFFER; /* It's not a front buffer */
2695 desc2.ddsCaps.dwCaps |= DDSCAPS_BACKBUFFER;
2698 hr = DD_OK;
2699 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2701 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_ALLFACES;
2702 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEZ;
2703 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2704 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEZ;
2705 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEZ;
2706 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2707 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEZ;
2708 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEY;
2709 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2710 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEY;
2711 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEY;
2712 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2713 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_POSITIVEY;
2714 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_NEGATIVEX;
2715 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces + 1, desc2, TRUE);
2716 desc2.ddsCaps.dwCaps2 &= ~DDSCAPS2_CUBEMAP_NEGATIVEX;
2717 desc2.ddsCaps.dwCaps2 |= DDSCAPS2_CUBEMAP_POSITIVEX;
2720 hr |= CreateAdditionalSurfaces(This, object, extra_surfaces, desc2, FALSE);
2721 if(hr != DD_OK)
2723 /* This destroys and possibly created surfaces too */
2724 IDirectDrawSurface_Release( ICOM_INTERFACE(object, IDirectDrawSurface7) );
2725 LeaveCriticalSection(&ddraw_cs);
2726 return hr;
2729 /* If the implementation is OpenGL and there's no d3ddevice, attach a d3ddevice
2730 * But attach the d3ddevice only if the currently created surface was
2731 * a primary surface (2D app in 3D mode) or a 3DDEVICE surface (3D app)
2732 * The only case I can think of where this doesn't apply is when a
2733 * 2D app was configured by the user to run with OpenGL and it didn't create
2734 * the render target as first surface. In this case the render target creation
2735 * will cause the 3D init.
2737 if( (This->ImplType == SURFACE_OPENGL) && !(This->d3d_initialized) &&
2738 desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE) )
2740 IDirectDrawSurfaceImpl *target = object, *surface;
2741 struct list *entry;
2743 /* Search for the primary to use as render target */
2744 LIST_FOR_EACH(entry, &This->surface_list)
2746 surface = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
2747 if((surface->surface_desc.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER)) ==
2748 (DDSCAPS_PRIMARYSURFACE | DDSCAPS_FRONTBUFFER))
2750 /* found */
2751 target = surface;
2752 TRACE("Using primary %p as render target\n", target);
2753 break;
2757 TRACE("(%p) Attaching a D3DDevice, rendertarget = %p\n", This, target);
2758 hr = IDirectDrawImpl_AttachD3DDevice(This, target);
2759 if(hr != D3D_OK)
2761 IDirectDrawSurfaceImpl *release_surf;
2762 ERR("IDirectDrawImpl_AttachD3DDevice failed, hr = %x\n", hr);
2763 *Surf = NULL;
2765 /* The before created surface structures are in an incomplete state here.
2766 * WineD3D holds the reference on the IParents, and it released them on the failure
2767 * already. So the regular release method implementation would fail on the attempt
2768 * to destroy either the IParents or the swapchain. So free the surface here.
2769 * The surface structure here is a list, not a tree, because onscreen targets
2770 * cannot be cube textures
2772 while(object)
2774 release_surf = object;
2775 object = object->complex_array[0];
2776 IDirectDrawSurfaceImpl_Destroy(release_surf);
2778 LeaveCriticalSection(&ddraw_cs);
2779 return hr;
2781 } else if(!(This->d3d_initialized) && desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
2782 IDirectDrawImpl_CreateGDISwapChain(This, object);
2785 /* Addref the ddraw interface to keep an reference for each surface */
2786 IDirectDraw7_AddRef(iface);
2787 object->ifaceToRelease = (IUnknown *) iface;
2789 /* Create a WineD3DTexture if a texture was requested */
2790 if(desc2.ddsCaps.dwCaps & DDSCAPS_TEXTURE)
2792 UINT levels;
2793 WINED3DFORMAT Format;
2794 WINED3DPOOL Pool = WINED3DPOOL_DEFAULT;
2796 This->tex_root = object;
2798 if(desc2.ddsCaps.dwCaps & DDSCAPS_MIPMAP)
2800 /* a mipmap is created, create enough levels */
2801 levels = desc2.u2.dwMipMapCount;
2803 else
2805 /* No mipmap is created, create one level */
2806 levels = 1;
2809 /* DDSCAPS_SYSTEMMEMORY textures are in WINED3DPOOL_SYSTEMMEM */
2810 if(DDSD->ddsCaps.dwCaps & DDSCAPS_SYSTEMMEMORY)
2812 Pool = WINED3DPOOL_SYSTEMMEM;
2814 /* Should I forward the MANAGED cap to the managed pool ? */
2816 /* Get the format. It's set already by CreateNewSurface */
2817 Format = PixelFormat_DD2WineD3D(&object->surface_desc.u4.ddpfPixelFormat);
2819 /* The surfaces are already created, the callback only
2820 * passes the IWineD3DSurface to WineD3D
2822 if(desc2.ddsCaps.dwCaps2 & DDSCAPS2_CUBEMAP)
2824 hr = IWineD3DDevice_CreateCubeTexture(This->wineD3DDevice,
2825 DDSD->dwWidth, /* Edgelength */
2826 levels,
2827 0, /* usage */
2828 Format,
2829 Pool,
2830 (IWineD3DCubeTexture **) &object->wineD3DTexture,
2831 0, /* SharedHandle */
2832 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2833 D3D7CB_CreateSurface);
2835 else
2837 hr = IWineD3DDevice_CreateTexture(This->wineD3DDevice,
2838 DDSD->dwWidth, DDSD->dwHeight,
2839 levels, /* MipMapCount = Levels */
2840 0, /* usage */
2841 Format,
2842 Pool,
2843 (IWineD3DTexture **) &object->wineD3DTexture,
2844 0, /* SharedHandle */
2845 (IUnknown *) ICOM_INTERFACE(object, IDirectDrawSurface7),
2846 D3D7CB_CreateSurface );
2848 This->tex_root = NULL;
2851 LeaveCriticalSection(&ddraw_cs);
2852 return hr;
2855 #define DDENUMSURFACES_SEARCHTYPE (DDENUMSURFACES_CANBECREATED|DDENUMSURFACES_DOESEXIST)
2856 #define DDENUMSURFACES_MATCHTYPE (DDENUMSURFACES_ALL|DDENUMSURFACES_MATCH|DDENUMSURFACES_NOMATCH)
2858 static BOOL
2859 Main_DirectDraw_DDPIXELFORMAT_Match(const DDPIXELFORMAT *requested,
2860 const DDPIXELFORMAT *provided)
2862 /* Some flags must be present in both or neither for a match. */
2863 static const DWORD must_match = DDPF_PALETTEINDEXED1 | DDPF_PALETTEINDEXED2
2864 | DDPF_PALETTEINDEXED4 | DDPF_PALETTEINDEXED8 | DDPF_FOURCC
2865 | DDPF_ZBUFFER | DDPF_STENCILBUFFER;
2867 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2868 return FALSE;
2870 if ((requested->dwFlags & must_match) != (provided->dwFlags & must_match))
2871 return FALSE;
2873 if (requested->dwFlags & DDPF_FOURCC)
2874 if (requested->dwFourCC != provided->dwFourCC)
2875 return FALSE;
2877 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_ALPHA
2878 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2879 if (requested->u1.dwRGBBitCount != provided->u1.dwRGBBitCount)
2880 return FALSE;
2882 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2883 |DDPF_LUMINANCE|DDPF_BUMPDUDV))
2884 if (requested->u2.dwRBitMask != provided->u2.dwRBitMask)
2885 return FALSE;
2887 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_ZBUFFER|DDPF_BUMPDUDV))
2888 if (requested->u3.dwGBitMask != provided->u3.dwGBitMask)
2889 return FALSE;
2891 /* I could be wrong about the bumpmapping. MSDN docs are vague. */
2892 if (requested->dwFlags & (DDPF_RGB|DDPF_YUV|DDPF_STENCILBUFFER
2893 |DDPF_BUMPDUDV))
2894 if (requested->u4.dwBBitMask != provided->u4.dwBBitMask)
2895 return FALSE;
2897 if (requested->dwFlags & (DDPF_ALPHAPIXELS|DDPF_ZPIXELS))
2898 if (requested->u5.dwRGBAlphaBitMask != provided->u5.dwRGBAlphaBitMask)
2899 return FALSE;
2901 return TRUE;
2904 static BOOL
2905 IDirectDrawImpl_DDSD_Match(const DDSURFACEDESC2* requested,
2906 const DDSURFACEDESC2* provided)
2908 struct compare_info
2910 DWORD flag;
2911 ptrdiff_t offset;
2912 size_t size;
2915 #define CMP(FLAG, FIELD) \
2916 { DDSD_##FLAG, offsetof(DDSURFACEDESC2, FIELD), \
2917 sizeof(((DDSURFACEDESC2 *)(NULL))->FIELD) }
2919 static const struct compare_info compare[] =
2921 CMP(ALPHABITDEPTH, dwAlphaBitDepth),
2922 CMP(BACKBUFFERCOUNT, dwBackBufferCount),
2923 CMP(CAPS, ddsCaps),
2924 CMP(CKDESTBLT, ddckCKDestBlt),
2925 CMP(CKDESTOVERLAY, u3 /* ddckCKDestOverlay */),
2926 CMP(CKSRCBLT, ddckCKSrcBlt),
2927 CMP(CKSRCOVERLAY, ddckCKSrcOverlay),
2928 CMP(HEIGHT, dwHeight),
2929 CMP(LINEARSIZE, u1 /* dwLinearSize */),
2930 CMP(LPSURFACE, lpSurface),
2931 CMP(MIPMAPCOUNT, u2 /* dwMipMapCount */),
2932 CMP(PITCH, u1 /* lPitch */),
2933 /* PIXELFORMAT: manual */
2934 CMP(REFRESHRATE, u2 /* dwRefreshRate */),
2935 CMP(TEXTURESTAGE, dwTextureStage),
2936 CMP(WIDTH, dwWidth),
2937 /* ZBUFFERBITDEPTH: "obsolete" */
2940 #undef CMP
2942 unsigned int i;
2944 if ((requested->dwFlags & provided->dwFlags) != requested->dwFlags)
2945 return FALSE;
2947 for (i=0; i < sizeof(compare)/sizeof(compare[0]); i++)
2949 if (requested->dwFlags & compare[i].flag
2950 && memcmp((const char *)provided + compare[i].offset,
2951 (const char *)requested + compare[i].offset,
2952 compare[i].size) != 0)
2953 return FALSE;
2956 if (requested->dwFlags & DDSD_PIXELFORMAT)
2958 if (!Main_DirectDraw_DDPIXELFORMAT_Match(&requested->u4.ddpfPixelFormat,
2959 &provided->u4.ddpfPixelFormat))
2960 return FALSE;
2963 return TRUE;
2966 #undef DDENUMSURFACES_SEARCHTYPE
2967 #undef DDENUMSURFACES_MATCHTYPE
2969 /*****************************************************************************
2970 * IDirectDraw7::EnumSurfaces
2972 * Loops through all surfaces attached to this device and calls the
2973 * application callback. This can't be relayed to WineD3DDevice,
2974 * because some WineD3DSurfaces' parents are IParent objects
2976 * Params:
2977 * Flags: Some filtering flags. See IDirectDrawImpl_EnumSurfacesCallback
2978 * DDSD: Description to filter for
2979 * Context: Application-provided pointer, it's passed unmodified to the
2980 * Callback function
2981 * Callback: Address to call for each surface
2983 * Returns:
2984 * DDERR_INVALIDPARAMS if the callback is NULL
2985 * DD_OK on success
2987 *****************************************************************************/
2988 static HRESULT WINAPI
2989 IDirectDrawImpl_EnumSurfaces(IDirectDraw7 *iface,
2990 DWORD Flags,
2991 DDSURFACEDESC2 *DDSD,
2992 void *Context,
2993 LPDDENUMSURFACESCALLBACK7 Callback)
2995 /* The surface enumeration is handled by WineDDraw,
2996 * because it keeps track of all surfaces attached to
2997 * it. The filtering is done by our callback function,
2998 * because WineDDraw doesn't handle ddraw-like surface
2999 * caps structures
3001 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3002 IDirectDrawSurfaceImpl *surf;
3003 BOOL all, nomatch;
3004 DDSURFACEDESC2 desc;
3005 struct list *entry, *entry2;
3007 all = Flags & DDENUMSURFACES_ALL;
3008 nomatch = Flags & DDENUMSURFACES_NOMATCH;
3010 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, DDSD, Context, Callback);
3011 EnterCriticalSection(&ddraw_cs);
3013 if(!Callback)
3015 LeaveCriticalSection(&ddraw_cs);
3016 return DDERR_INVALIDPARAMS;
3019 /* Use the _SAFE enumeration, the app may destroy enumerated surfaces */
3020 LIST_FOR_EACH_SAFE(entry, entry2, &This->surface_list)
3022 surf = LIST_ENTRY(entry, IDirectDrawSurfaceImpl, surface_list_entry);
3023 if (all || (nomatch != IDirectDrawImpl_DDSD_Match(DDSD, &surf->surface_desc)))
3025 desc = surf->surface_desc;
3026 IDirectDrawSurface7_AddRef(ICOM_INTERFACE(surf, IDirectDrawSurface7));
3027 if(Callback( ICOM_INTERFACE(surf, IDirectDrawSurface7), &desc, Context) != DDENUMRET_OK)
3029 LeaveCriticalSection(&ddraw_cs);
3030 return DD_OK;
3034 LeaveCriticalSection(&ddraw_cs);
3035 return DD_OK;
3038 static HRESULT WINAPI
3039 findRenderTarget(IDirectDrawSurface7 *surface,
3040 DDSURFACEDESC2 *desc,
3041 void *ctx)
3043 IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surface);
3044 IDirectDrawSurfaceImpl **target = (IDirectDrawSurfaceImpl **) ctx;
3046 if(!surf->isRenderTarget) {
3047 *target = surf;
3048 IDirectDrawSurface7_Release(surface);
3049 return DDENUMRET_CANCEL;
3052 /* Recurse into the surface tree */
3053 IDirectDrawSurface7_EnumAttachedSurfaces(surface, ctx, findRenderTarget);
3055 IDirectDrawSurface7_Release(surface);
3056 if(*target) return DDENUMRET_CANCEL;
3057 else return DDENUMRET_OK; /* Continue with the next neighbor surface */
3060 /*****************************************************************************
3061 * D3D7CB_CreateRenderTarget
3063 * Callback called by WineD3D to create Surfaces for render target usage
3064 * This function takes the D3D target from the IDirectDrawImpl structure,
3065 * and returns the WineD3DSurface. To avoid double usage, the surface
3066 * is marked as render target afterwards
3068 * Params
3069 * device: The WineD3DDevice's parent
3070 * Width, Height, Format: Dimensions and pixelformat of the render target
3071 * Ignored, because the surface already exists
3072 * MultiSample, MultisampleQuality, Lockable: Ignored for the same reason
3073 * Lockable: ignored
3074 * ppSurface: Address to pass the surface pointer back at
3075 * pSharedHandle: Ignored
3077 * Returns:
3078 * Always returns D3D_OK
3080 *****************************************************************************/
3081 static HRESULT WINAPI
3082 D3D7CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior,
3083 UINT Width, UINT Height,
3084 WINED3DFORMAT Format,
3085 WINED3DMULTISAMPLE_TYPE MultiSample,
3086 DWORD MultisampleQuality,
3087 BOOL Lockable,
3088 IWineD3DSurface** ppSurface,
3089 HANDLE* pSharedHandle)
3091 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3092 IDirectDrawSurfaceImpl *d3dSurface = This->d3d_target, *target = NULL;
3093 TRACE("(%p) call back\n", device);
3095 if(d3dSurface->isRenderTarget)
3097 IDirectDrawSurface7_EnumAttachedSurfaces(ICOM_INTERFACE(d3dSurface, IDirectDrawSurface7),
3098 &target, findRenderTarget);
3100 else
3102 target = d3dSurface;
3105 if(!target)
3107 target = This->d3d_target;
3108 ERR(" (%p) : No DirectDrawSurface found to create the back buffer. Using the front buffer as back buffer. Uncertain consequences\n", This);
3111 /* TODO: Return failure if the dimensions do not match, but this shouldn't happen */
3113 *ppSurface = target->WineD3DSurface;
3114 target->isRenderTarget = TRUE;
3115 TRACE("Returning wineD3DSurface %p, it belongs to surface %p\n", *ppSurface, d3dSurface);
3116 return D3D_OK;
3119 static HRESULT WINAPI
3120 D3D7CB_CreateDepthStencilSurface(IUnknown *device,
3121 IUnknown *pSuperior,
3122 UINT Width,
3123 UINT Height,
3124 WINED3DFORMAT Format,
3125 WINED3DMULTISAMPLE_TYPE MultiSample,
3126 DWORD MultisampleQuality,
3127 BOOL Discard,
3128 IWineD3DSurface** ppSurface,
3129 HANDLE* pSharedHandle)
3131 /* Create a Depth Stencil surface to make WineD3D happy */
3132 HRESULT hr = D3D_OK;
3133 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3134 DDSURFACEDESC2 ddsd;
3136 TRACE("(%p) call back\n", device);
3138 *ppSurface = NULL;
3140 /* Create a DirectDraw surface */
3141 memset(&ddsd, 0, sizeof(ddsd));
3142 ddsd.dwSize = sizeof(ddsd);
3143 ddsd.u4.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
3144 ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
3145 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
3146 ddsd.dwHeight = Height;
3147 ddsd.dwWidth = Width;
3148 if(Format != 0)
3150 PixelFormat_WineD3DtoDD(&ddsd.u4.ddpfPixelFormat, Format);
3152 else
3154 ddsd.dwFlags ^= DDSD_PIXELFORMAT;
3157 This->depthstencil = TRUE;
3158 hr = IDirectDraw7_CreateSurface((IDirectDraw7 *) This,
3159 &ddsd,
3160 (IDirectDrawSurface7 **) &This->DepthStencilBuffer,
3161 NULL);
3162 This->depthstencil = FALSE;
3163 if(FAILED(hr))
3165 ERR(" (%p) Creating a DepthStencil Surface failed, result = %x\n", This, hr);
3166 return hr;
3168 *ppSurface = This->DepthStencilBuffer->WineD3DSurface;
3169 return D3D_OK;
3172 /*****************************************************************************
3173 * D3D7CB_CreateAdditionalSwapChain
3175 * Callback function for WineD3D which creates a new WineD3DSwapchain
3176 * interface. It also creates an IParent interface to store that pointer,
3177 * so the WineD3DSwapchain has a parent and can be released when the D3D
3178 * device is destroyed
3179 *****************************************************************************/
3180 static HRESULT WINAPI
3181 D3D7CB_CreateAdditionalSwapChain(IUnknown *device,
3182 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
3183 IWineD3DSwapChain ** ppSwapChain)
3185 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, device);
3186 IParentImpl *object = NULL;
3187 HRESULT res = D3D_OK;
3188 IWineD3DSwapChain *swapchain;
3189 IDirectDrawSurfaceImpl *iterator;
3190 TRACE("(%p) call back\n", device);
3192 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IParentImpl));
3193 if (NULL == object)
3195 FIXME("Allocation of memory failed\n");
3196 *ppSwapChain = NULL;
3197 return DDERR_OUTOFVIDEOMEMORY;
3200 ICOM_INIT_INTERFACE(object, IParent, IParent_Vtbl);
3201 object->ref = 1;
3203 res = IWineD3DDevice_CreateAdditionalSwapChain(This->wineD3DDevice,
3204 pPresentationParameters,
3205 &swapchain,
3206 (IUnknown*) ICOM_INTERFACE(object, IParent),
3207 D3D7CB_CreateRenderTarget,
3208 D3D7CB_CreateDepthStencilSurface,
3209 This->ImplType);
3210 if (res != D3D_OK)
3212 FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
3213 HeapFree(GetProcessHeap(), 0 , object);
3214 *ppSwapChain = NULL;
3216 else
3218 *ppSwapChain = swapchain;
3219 object->child = (IUnknown *) swapchain;
3220 This->d3d_target->wineD3DSwapChain = swapchain;
3221 iterator = This->d3d_target->complex_array[0];
3222 while(iterator) {
3223 iterator->wineD3DSwapChain = swapchain;
3224 iterator = iterator->complex_array[0];
3228 return res;
3231 static HRESULT WINAPI IDirectDrawImpl_CreateGDISwapChain(IDirectDrawImpl *This,
3232 IDirectDrawSurfaceImpl *primary) {
3233 HRESULT hr;
3234 WINED3DPRESENT_PARAMETERS presentation_parameters;
3235 HWND window;
3237 window = This->dest_window;
3239 memset(&presentation_parameters, 0, sizeof(presentation_parameters));
3241 /* Use the surface description for the device parameters, not the
3242 * Device settings. The app might render to an offscreen surface
3244 presentation_parameters.BackBufferWidth = primary->surface_desc.dwWidth;
3245 presentation_parameters.BackBufferHeight = primary->surface_desc.dwHeight;
3246 presentation_parameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3247 presentation_parameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3248 presentation_parameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3249 presentation_parameters.MultiSampleQuality = 0;
3250 presentation_parameters.SwapEffect = WINED3DSWAPEFFECT_FLIP;
3251 presentation_parameters.hDeviceWindow = window;
3252 presentation_parameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3253 presentation_parameters.EnableAutoDepthStencil = FALSE; /* Not on GDI swapchains */
3254 presentation_parameters.AutoDepthStencilFormat = 0;
3255 presentation_parameters.Flags = 0;
3256 presentation_parameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3257 presentation_parameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3259 This->d3d_target = primary;
3260 hr = IWineD3DDevice_InitGDI(This->wineD3DDevice,
3261 &presentation_parameters,
3262 D3D7CB_CreateAdditionalSwapChain);
3263 This->d3d_target = NULL;
3265 if (hr != D3D_OK)
3267 FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
3268 primary->wineD3DSwapChain = NULL;
3270 return hr;
3273 /*****************************************************************************
3274 * IDirectDrawImpl_AttachD3DDevice
3276 * Initializes the D3D capabilities of WineD3D
3278 * Params:
3279 * primary: The primary surface for D3D
3281 * Returns
3282 * DD_OK on success,
3283 * DDERR_* otherwise
3285 *****************************************************************************/
3286 static HRESULT WINAPI
3287 IDirectDrawImpl_AttachD3DDevice(IDirectDrawImpl *This,
3288 IDirectDrawSurfaceImpl *primary)
3290 HRESULT hr;
3291 HWND window = This->dest_window;
3293 WINED3DPRESENT_PARAMETERS localParameters;
3295 TRACE("(%p)->(%p)\n", This, primary);
3297 /* If there's no window, create a hidden window. WineD3D needs it */
3298 if(window == 0 || window == GetDesktopWindow())
3300 window = CreateWindowExA(0, This->classname, "Hidden D3D Window",
3301 WS_DISABLED, 0, 0,
3302 GetSystemMetrics(SM_CXSCREEN),
3303 GetSystemMetrics(SM_CYSCREEN),
3304 NULL, NULL, GetModuleHandleA(0), NULL);
3306 ShowWindow(window, SW_HIDE); /* Just to be sure */
3307 WARN("(%p) No window for the Direct3DDevice, created a hidden window. HWND=%p\n", This, window);
3309 else
3311 TRACE("(%p) Using existing window %p for Direct3D rendering\n", This, window);
3313 This->d3d_window = window;
3315 /* Store the future Render Target surface */
3316 This->d3d_target = primary;
3318 /* Use the surface description for the device parameters, not the
3319 * Device settings. The app might render to an offscreen surface
3321 localParameters.BackBufferWidth = primary->surface_desc.dwWidth;
3322 localParameters.BackBufferHeight = primary->surface_desc.dwHeight;
3323 localParameters.BackBufferFormat = PixelFormat_DD2WineD3D(&primary->surface_desc.u4.ddpfPixelFormat);
3324 localParameters.BackBufferCount = (primary->surface_desc.dwFlags & DDSD_BACKBUFFERCOUNT) ? primary->surface_desc.dwBackBufferCount : 0;
3325 localParameters.MultiSampleType = WINED3DMULTISAMPLE_NONE;
3326 localParameters.MultiSampleQuality = 0;
3327 localParameters.SwapEffect = WINED3DSWAPEFFECT_COPY;
3328 localParameters.hDeviceWindow = window;
3329 localParameters.Windowed = !(This->cooperative_level & DDSCL_FULLSCREEN);
3330 localParameters.EnableAutoDepthStencil = TRUE;
3331 localParameters.AutoDepthStencilFormat = WINED3DFMT_D16;
3332 localParameters.Flags = 0;
3333 localParameters.FullScreen_RefreshRateInHz = WINED3DPRESENT_RATE_DEFAULT; /* Default rate: It's already set */
3334 localParameters.PresentationInterval = WINED3DPRESENT_INTERVAL_DEFAULT;
3336 TRACE("Passing mode %d\n", localParameters.BackBufferFormat);
3338 /* Set this NOW, otherwise creating the depth stencil surface will cause a
3339 * recursive loop until ram or emulated video memory is full
3341 This->d3d_initialized = TRUE;
3343 hr = IWineD3DDevice_Init3D(This->wineD3DDevice,
3344 &localParameters,
3345 D3D7CB_CreateAdditionalSwapChain);
3346 if(FAILED(hr))
3348 This->d3d_target = NULL;
3349 This->d3d_initialized = FALSE;
3350 return hr;
3353 This->declArraySize = 2;
3354 This->decls = HeapAlloc(GetProcessHeap(),
3355 HEAP_ZERO_MEMORY,
3356 sizeof(*This->decls) * This->declArraySize);
3357 if(!This->decls)
3359 ERR("Error allocating an array for the converted vertex decls\n");
3360 This->declArraySize = 0;
3361 hr = IWineD3DDevice_Uninit3D(This->wineD3DDevice,
3362 D3D7CB_DestroyDepthStencilSurface,
3363 D3D7CB_DestroySwapChain);
3364 return E_OUTOFMEMORY;
3367 /* Create an Index Buffer parent */
3368 TRACE("(%p) Successfully initialized 3D\n", This);
3369 return DD_OK;
3372 /*****************************************************************************
3373 * DirectDrawCreateClipper (DDRAW.@)
3375 * Creates a new IDirectDrawClipper object.
3377 * Params:
3378 * Clipper: Address to write the interface pointer to
3379 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3380 * NULL
3382 * Returns:
3383 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3384 * E_OUTOFMEMORY if allocating the object failed
3386 *****************************************************************************/
3387 HRESULT WINAPI
3388 DirectDrawCreateClipper(DWORD Flags,
3389 LPDIRECTDRAWCLIPPER *Clipper,
3390 IUnknown *UnkOuter)
3392 IDirectDrawClipperImpl* object;
3393 TRACE("(%08x,%p,%p)\n", Flags, Clipper, UnkOuter);
3395 EnterCriticalSection(&ddraw_cs);
3396 if (UnkOuter != NULL)
3398 LeaveCriticalSection(&ddraw_cs);
3399 return CLASS_E_NOAGGREGATION;
3402 if (!LoadWineD3D())
3404 LeaveCriticalSection(&ddraw_cs);
3405 return DDERR_NODIRECTDRAWSUPPORT;
3408 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3409 sizeof(IDirectDrawClipperImpl));
3410 if (object == NULL)
3412 LeaveCriticalSection(&ddraw_cs);
3413 return E_OUTOFMEMORY;
3416 ICOM_INIT_INTERFACE(object, IDirectDrawClipper, IDirectDrawClipper_Vtbl);
3417 object->ref = 1;
3418 object->wineD3DClipper = pWineDirect3DCreateClipper((IUnknown *) object);
3419 if(!object->wineD3DClipper)
3421 HeapFree(GetProcessHeap(), 0, object);
3422 LeaveCriticalSection(&ddraw_cs);
3423 return E_OUTOFMEMORY;
3426 *Clipper = (IDirectDrawClipper *) object;
3427 LeaveCriticalSection(&ddraw_cs);
3428 return DD_OK;
3431 /*****************************************************************************
3432 * IDirectDraw7::CreateClipper
3434 * Creates a DDraw clipper. See DirectDrawCreateClipper for details
3436 *****************************************************************************/
3437 static HRESULT WINAPI
3438 IDirectDrawImpl_CreateClipper(IDirectDraw7 *iface,
3439 DWORD Flags,
3440 IDirectDrawClipper **Clipper,
3441 IUnknown *UnkOuter)
3443 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3444 TRACE("(%p)->(%x,%p,%p)\n", This, Flags, Clipper, UnkOuter);
3445 return DirectDrawCreateClipper(Flags, Clipper, UnkOuter);
3448 /*****************************************************************************
3449 * IDirectDraw7::CreatePalette
3451 * Creates a new IDirectDrawPalette object
3453 * Params:
3454 * Flags: The flags for the new clipper
3455 * ColorTable: Color table to assign to the new clipper
3456 * Palette: Address to write the interface pointer to
3457 * UnkOuter: For aggregation support, which ddraw doesn't have. Has to be
3458 * NULL
3460 * Returns:
3461 * CLASS_E_NOAGGREGATION if UnkOuter != NULL
3462 * E_OUTOFMEMORY if allocating the object failed
3464 *****************************************************************************/
3465 static HRESULT WINAPI
3466 IDirectDrawImpl_CreatePalette(IDirectDraw7 *iface,
3467 DWORD Flags,
3468 PALETTEENTRY *ColorTable,
3469 IDirectDrawPalette **Palette,
3470 IUnknown *pUnkOuter)
3472 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3473 IDirectDrawPaletteImpl *object;
3474 HRESULT hr = DDERR_GENERIC;
3475 TRACE("(%p)->(%x,%p,%p,%p)\n", This, Flags, ColorTable, Palette, pUnkOuter);
3477 EnterCriticalSection(&ddraw_cs);
3478 if(pUnkOuter != NULL)
3480 WARN("pUnkOuter is %p, returning CLASS_E_NOAGGREGATION\n", pUnkOuter);
3481 LeaveCriticalSection(&ddraw_cs);
3482 return CLASS_E_NOAGGREGATION;
3485 /* The refcount test shows that a cooplevel is required for this */
3486 if(!This->cooperative_level)
3488 WARN("No cooperative level set, returning DDERR_NOCOOPERATIVELEVELSET\n");
3489 LeaveCriticalSection(&ddraw_cs);
3490 return DDERR_NOCOOPERATIVELEVELSET;
3493 object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
3494 if(!object)
3496 ERR("Out of memory when allocating memory for a palette implementation\n");
3497 LeaveCriticalSection(&ddraw_cs);
3498 return E_OUTOFMEMORY;
3501 ICOM_INIT_INTERFACE(object, IDirectDrawPalette, IDirectDrawPalette_Vtbl);
3502 object->ref = 1;
3503 object->ddraw_owner = This;
3505 hr = IWineD3DDevice_CreatePalette(This->wineD3DDevice, Flags, ColorTable, &object->wineD3DPalette, (IUnknown *) ICOM_INTERFACE(object, IDirectDrawPalette) );
3506 if(hr != DD_OK)
3508 HeapFree(GetProcessHeap(), 0, object);
3509 LeaveCriticalSection(&ddraw_cs);
3510 return hr;
3513 IDirectDraw7_AddRef(iface);
3514 object->ifaceToRelease = (IUnknown *) iface;
3515 *Palette = ICOM_INTERFACE(object, IDirectDrawPalette);
3516 LeaveCriticalSection(&ddraw_cs);
3517 return DD_OK;
3520 /*****************************************************************************
3521 * IDirectDraw7::DuplicateSurface
3523 * Duplicates a surface. The surface memory points to the same memory as
3524 * the original surface, and it's released when the last surface referencing
3525 * it is released. I guess that's beyond Wine's surface management right now
3526 * (Idea: create a new DDraw surface with the same WineD3DSurface. I need a
3527 * test application to implement this)
3529 * Params:
3530 * Src: Address of the source surface
3531 * Dest: Address to write the new surface pointer to
3533 * Returns:
3534 * See IDirectDraw7::CreateSurface
3536 *****************************************************************************/
3537 static HRESULT WINAPI
3538 IDirectDrawImpl_DuplicateSurface(IDirectDraw7 *iface,
3539 IDirectDrawSurface7 *Src,
3540 IDirectDrawSurface7 **Dest)
3542 ICOM_THIS_FROM(IDirectDrawImpl, IDirectDraw7, iface);
3543 IDirectDrawSurfaceImpl *Surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Src);
3545 FIXME("(%p)->(%p,%p)\n", This, Surf, Dest);
3547 /* For now, simply create a new, independent surface */
3548 return IDirectDraw7_CreateSurface(iface,
3549 &Surf->surface_desc,
3550 Dest,
3551 NULL);
3554 /*****************************************************************************
3555 * IDirectDraw7 VTable
3556 *****************************************************************************/
3557 const IDirectDraw7Vtbl IDirectDraw7_Vtbl =
3559 /*** IUnknown ***/
3560 IDirectDrawImpl_QueryInterface,
3561 IDirectDrawImpl_AddRef,
3562 IDirectDrawImpl_Release,
3563 /*** IDirectDraw ***/
3564 IDirectDrawImpl_Compact,
3565 IDirectDrawImpl_CreateClipper,
3566 IDirectDrawImpl_CreatePalette,
3567 IDirectDrawImpl_CreateSurface,
3568 IDirectDrawImpl_DuplicateSurface,
3569 IDirectDrawImpl_EnumDisplayModes,
3570 IDirectDrawImpl_EnumSurfaces,
3571 IDirectDrawImpl_FlipToGDISurface,
3572 IDirectDrawImpl_GetCaps,
3573 IDirectDrawImpl_GetDisplayMode,
3574 IDirectDrawImpl_GetFourCCCodes,
3575 IDirectDrawImpl_GetGDISurface,
3576 IDirectDrawImpl_GetMonitorFrequency,
3577 IDirectDrawImpl_GetScanLine,
3578 IDirectDrawImpl_GetVerticalBlankStatus,
3579 IDirectDrawImpl_Initialize,
3580 IDirectDrawImpl_RestoreDisplayMode,
3581 IDirectDrawImpl_SetCooperativeLevel,
3582 IDirectDrawImpl_SetDisplayMode,
3583 IDirectDrawImpl_WaitForVerticalBlank,
3584 /*** IDirectDraw2 ***/
3585 IDirectDrawImpl_GetAvailableVidMem,
3586 /*** IDirectDraw7 ***/
3587 IDirectDrawImpl_GetSurfaceFromDC,
3588 IDirectDrawImpl_RestoreAllSurfaces,
3589 IDirectDrawImpl_TestCooperativeLevel,
3590 IDirectDrawImpl_GetDeviceIdentifier,
3591 /*** IDirectDraw7 ***/
3592 IDirectDrawImpl_StartModeTest,
3593 IDirectDrawImpl_EvaluateMode
3596 /*****************************************************************************
3597 * IDirectDrawImpl_FindDecl
3599 * Finds the WineD3D vertex declaration for a specific fvf, and creates one
3600 * if none was found.
3602 * This function is in ddraw.c and the DDraw object space because D3D7
3603 * vertex buffers are created using the IDirect3D interface to the ddraw
3604 * object, so they can be valid across D3D devices(theoretically. The ddraw
3605 * object also owns the wined3d device
3607 * Parameters:
3608 * This: Device
3609 * fvf: Fvf to find the decl for
3611 * Returns:
3612 * NULL in case of an error, the IWineD3DVertexDeclaration interface for the
3613 * fvf otherwise.
3615 *****************************************************************************/
3616 IWineD3DVertexDeclaration *
3617 IDirectDrawImpl_FindDecl(IDirectDrawImpl *This,
3618 DWORD fvf)
3620 HRESULT hr;
3621 IWineD3DVertexDeclaration* pDecl = NULL;
3622 int p, low, high; /* deliberately signed */
3623 struct FvfToDecl *convertedDecls = This->decls;
3625 TRACE("Searching for declaration for fvf %08x... ", fvf);
3627 low = 0;
3628 high = This->numConvertedDecls - 1;
3629 while(low <= high) {
3630 p = (low + high) >> 1;
3631 TRACE("%d ", p);
3632 if(convertedDecls[p].fvf == fvf) {
3633 TRACE("found %p\n", convertedDecls[p].decl);
3634 return convertedDecls[p].decl;
3635 } else if(convertedDecls[p].fvf < fvf) {
3636 low = p + 1;
3637 } else {
3638 high = p - 1;
3641 TRACE("not found. Creating and inserting at position %d.\n", low);
3643 hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->wineD3DDevice,
3644 &pDecl,
3645 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7),
3646 fvf);
3647 if (hr != S_OK) return NULL;
3649 if(This->declArraySize == This->numConvertedDecls) {
3650 int grow = max(This->declArraySize / 2, 8);
3651 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
3652 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
3653 if(!convertedDecls) {
3654 /* This will destroy it */
3655 IWineD3DVertexDeclaration_Release(pDecl);
3656 return NULL;
3658 This->decls = convertedDecls;
3659 This->declArraySize += grow;
3662 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
3663 convertedDecls[low].decl = pDecl;
3664 convertedDecls[low].fvf = fvf;
3665 This->numConvertedDecls++;
3667 TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
3668 return pDecl;