1 /* DirectDraw Base Functions
3 * Copyright 1997-1999 Marcus Meissner
4 * Copyright 1998 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2006 Stefan Dösinger
8 * This file contains the (internal) driver registration functions,
9 * driver enumeration APIs and DirectDraw creation functions.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/port.h"
28 #include "wine/debug.h"
42 #include "wine/exception.h"
49 #include "ddraw_private.h"
51 typedef IWineD3D* (WINAPI *fnWineDirect3DCreate)(UINT, UINT, IUnknown *);
53 static HMODULE hWineD3D = (HMODULE) -1;
54 static fnWineDirect3DCreate pWineDirect3DCreate;
56 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
58 /* The configured default surface */
59 WINED3DSURFTYPE DefaultSurfaceType = SURFACE_UNKNOWN;
61 /* DDraw list and critical section */
62 static struct list global_ddraw_list = LIST_INIT(global_ddraw_list);
64 static CRITICAL_SECTION ddraw_list_cs;
65 static CRITICAL_SECTION_DEBUG ddraw_list_cs_debug =
68 { &ddraw_list_cs_debug.ProcessLocksList,
69 &ddraw_list_cs_debug.ProcessLocksList },
70 0, 0, { (DWORD_PTR)(__FILE__ ": ddraw_list_cs") }
72 static CRITICAL_SECTION ddraw_list_cs = { &ddraw_list_cs_debug, -1, 0, 0, 0, 0 };
74 /***********************************************************************
76 * Helper function for DirectDrawCreate and friends
77 * Creates a new DDraw interface with the given REFIID
79 * Interfaces that can be created:
80 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
81 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
82 * IDirect3D interfaces?)
85 * guid: ID of the requested driver, NULL for the default driver.
86 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
87 * DD: Used to return the pointer to the created object
88 * UnkOuter: For aggregation, which is unsupported. Must be NULL
89 * iid: requested version ID.
92 * DD_OK if the Interface was created successfully
93 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
94 * E_OUTOFMEMORY if some allocation failed
96 ***********************************************************************/
98 DDRAW_Create(const GUID *guid,
103 IDirectDrawImpl *This = NULL;
105 IWineD3D *wineD3D = NULL;
106 IWineD3DDevice *wineD3DDevice = NULL;
108 WINED3DDEVTYPE devicetype;
110 TRACE("(%s,%p,%p)\n", debugstr_guid(guid), DD, UnkOuter);
114 /* We don't care about this guids. Well, there's no special guid anyway
117 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
119 /* Use the reference device id. This doesn't actually change anything,
120 * WineD3D always uses OpenGL for D3D rendering. One could make it request
123 devicetype = WINED3DDEVTYPE_REF;
125 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
127 devicetype = WINED3DDEVTYPE_HAL;
134 /* DDraw doesn't support aggregation, according to msdn */
135 if (UnkOuter != NULL)
136 return CLASS_E_NOAGGREGATION;
138 /* DirectDraw creation comes here */
139 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawImpl));
142 ERR("Out of memory when creating DirectDraw\n");
143 return E_OUTOFMEMORY;
147 * IDirectDraw and IDirect3D are the same object,
148 * QueryInterface is used to get other interfaces.
150 ICOM_INIT_INTERFACE(This, IDirectDraw, IDirectDraw1_Vtbl);
151 ICOM_INIT_INTERFACE(This, IDirectDraw2, IDirectDraw2_Vtbl);
152 ICOM_INIT_INTERFACE(This, IDirectDraw3, IDirectDraw3_Vtbl);
153 ICOM_INIT_INTERFACE(This, IDirectDraw4, IDirectDraw4_Vtbl);
154 ICOM_INIT_INTERFACE(This, IDirectDraw7, IDirectDraw7_Vtbl);
155 ICOM_INIT_INTERFACE(This, IDirect3D, IDirect3D1_Vtbl);
156 ICOM_INIT_INTERFACE(This, IDirect3D2, IDirect3D2_Vtbl);
157 ICOM_INIT_INTERFACE(This, IDirect3D3, IDirect3D3_Vtbl);
158 ICOM_INIT_INTERFACE(This, IDirect3D7, IDirect3D7_Vtbl);
160 /* See comments in IDirectDrawImpl_CreateNewSurface for a description
162 * Read from a registry key, should add a winecfg option later
164 This->ImplType = DefaultSurfaceType;
166 /* Get the current screen settings */
168 This->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
170 This->orig_width = GetSystemMetrics(SM_CXSCREEN);
171 This->orig_height = GetSystemMetrics(SM_CYSCREEN);
173 if (hWineD3D == (HMODULE) -1)
175 hWineD3D = LoadLibraryA("wined3d");
177 pWineDirect3DCreate = (fnWineDirect3DCreate) GetProcAddress(hWineD3D, "WineDirect3DCreate");
182 ERR("Couldn't load WineD3D - OpenGL libs not present?\n");
183 hr = DDERR_NODIRECTDRAWSUPPORT;
187 /* Initialize WineD3D
189 * All Rendering (2D and 3D) is relayed to WineD3D,
190 * but DirectDraw specific management, like DDSURFACEDESC and DDPIXELFORMAT
191 * structure handling is handled in this lib.
193 wineD3D = pWineDirect3DCreate(0 /* SDKVersion */, 7 /* DXVersion */, (IUnknown *) This /* Parent */);
196 ERR("Failed to initialise WineD3D\n");
200 This->wineD3D = wineD3D;
201 TRACE("WineD3D created at %p\n", wineD3D);
203 /* Initialized member...
205 * It is set to false at creation time, and set to true in
206 * IDirectDraw7::Initialize. Its sole purpose is to return DD_OK on
207 * initialize only once
209 This->initialized = FALSE;
211 /* Initialize WineD3DDevice
213 * It is used for screen setup, surface and palette creation
214 * When a Direct3DDevice7 is created, the D3D capabilities of WineD3D are
217 hr = IWineD3D_CreateDevice(wineD3D,
218 0 /*D3D_ADAPTER_DEFAULT*/,
220 NULL, /* FocusWindow, don't know yet */
221 0, /* BehaviorFlags */
223 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7));
226 ERR("Failed to create a wineD3DDevice, result = %x\n", hr);
229 This->wineD3DDevice = wineD3DDevice;
230 TRACE("wineD3DDevice created at %p\n", This->wineD3DDevice);
232 /* Register the window class
234 * It is used to create a hidden window for D3D
235 * rendering, if the application didn't pass one.
236 * It can also be used for Creating a device window
237 * from SetCooperativeLevel
239 * The name: DDRAW_<address>. The classname is
240 * 32 bit long, so a 64 bit address will fit nicely
241 * (Will this be compiled for 64 bit anyway?)
244 sprintf(This->classname, "DDRAW_%p", This);
246 memset(&This->wnd_class, 0, sizeof(This->wnd_class));
247 This->wnd_class.style = CS_HREDRAW | CS_VREDRAW;
248 This->wnd_class.lpfnWndProc = DefWindowProcA;
249 This->wnd_class.cbClsExtra = 0;
250 This->wnd_class.cbWndExtra = 0;
251 This->wnd_class.hInstance = GetModuleHandleA(0);
252 This->wnd_class.hIcon = 0;
253 This->wnd_class.hCursor = 0;
254 This->wnd_class.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
255 This->wnd_class.lpszMenuName = NULL;
256 This->wnd_class.lpszClassName = This->classname;
257 if(!RegisterClassA(&This->wnd_class))
259 ERR("RegisterClassA failed!\n");
263 /* Get the amount of video memory */
264 This->total_vidmem = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
266 /* Initialize the caps */
267 This->caps.dwSize = sizeof(This->caps);
268 /* do not report DDCAPS_OVERLAY and friends since we don't support overlays */
269 #define BLIT_CAPS (DDCAPS_BLT | DDCAPS_BLTCOLORFILL | DDCAPS_BLTDEPTHFILL \
270 | DDCAPS_BLTSTRETCH | DDCAPS_CANBLTSYSMEM | DDCAPS_CANCLIP \
271 | DDCAPS_CANCLIPSTRETCHED | DDCAPS_COLORKEY \
272 | DDCAPS_COLORKEYHWASSIST | DDCAPS_ALIGNBOUNDARYSRC )
273 #define CKEY_CAPS (DDCKEYCAPS_DESTBLT | DDCKEYCAPS_SRCBLT)
274 #define FX_CAPS (DDFXCAPS_BLTALPHA | DDFXCAPS_BLTMIRRORLEFTRIGHT \
275 | DDFXCAPS_BLTMIRRORUPDOWN | DDFXCAPS_BLTROTATION90 \
276 | DDFXCAPS_BLTSHRINKX | DDFXCAPS_BLTSHRINKXN \
277 | DDFXCAPS_BLTSHRINKY | DDFXCAPS_BLTSHRINKXN \
278 | DDFXCAPS_BLTSTRETCHX | DDFXCAPS_BLTSTRETCHXN \
279 | DDFXCAPS_BLTSTRETCHY | DDFXCAPS_BLTSTRETCHYN)
280 This->caps.dwCaps |= DDCAPS_GDI | DDCAPS_PALETTE | BLIT_CAPS;
282 This->caps.dwCaps2 |= DDCAPS2_CERTIFIED | DDCAPS2_NOPAGELOCKREQUIRED |
283 DDCAPS2_PRIMARYGAMMA | DDCAPS2_WIDESURFACES |
284 DDCAPS2_CANRENDERWINDOWED;
285 This->caps.dwCKeyCaps |= CKEY_CAPS;
286 This->caps.dwFXCaps |= FX_CAPS;
287 This->caps.dwPalCaps |= DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE;
288 This->caps.dwVidMemTotal = This->total_vidmem;
289 This->caps.dwVidMemFree = This->total_vidmem;
290 This->caps.dwSVBCaps |= BLIT_CAPS;
291 This->caps.dwSVBCKeyCaps |= CKEY_CAPS;
292 This->caps.dwSVBFXCaps |= FX_CAPS;
293 This->caps.dwVSBCaps |= BLIT_CAPS;
294 This->caps.dwVSBCKeyCaps |= CKEY_CAPS;
295 This->caps.dwVSBFXCaps |= FX_CAPS;
296 This->caps.dwSSBCaps |= BLIT_CAPS;
297 This->caps.dwSSBCKeyCaps |= CKEY_CAPS;
298 This->caps.dwSSBFXCaps |= FX_CAPS;
299 This->caps.ddsCaps.dwCaps |= DDSCAPS_ALPHA | DDSCAPS_BACKBUFFER |
300 DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER |
301 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_PALETTE |
302 DDSCAPS_PRIMARYSURFACE | DDSCAPS_SYSTEMMEMORY |
303 DDSCAPS_VIDEOMEMORY | DDSCAPS_VISIBLE;
304 /* Hacks for D3D code */
305 /* TODO: Check if WineD3D has 3D enabled
306 Need opengl surfaces or auto for 3D
308 if(This->ImplType == 0 || This->ImplType == SURFACE_OPENGL)
310 This->caps.dwCaps |= DDCAPS_3D;
311 This->caps.ddsCaps.dwCaps |= DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER;
313 This->caps.ddsOldCaps.dwCaps = This->caps.ddsCaps.dwCaps;
319 list_init(&This->surface_list);
321 EnterCriticalSection(&ddraw_list_cs);
322 list_add_head(&global_ddraw_list, &This->ddraw_list_entry);
323 LeaveCriticalSection(&ddraw_list_cs);
325 This->decls = HeapAlloc(GetProcessHeap(), 0, 0);
328 ERR("Error allocating an empty array for the converted vertex decls\n");
332 /* Call QueryInterface to get the pointer to the requested interface. This also initializes
333 * The required refcount
335 hr = IDirectDraw7_QueryInterface( ICOM_INTERFACE(This, IDirectDraw7), iid, DD);
336 if(SUCCEEDED(hr)) return DD_OK;
339 /* Let's hope we never need this ;) */
340 if(wineD3DDevice) IWineD3DDevice_Release(wineD3DDevice);
341 if(wineD3D) IWineD3D_Release(wineD3D);
342 if(This) HeapFree(GetProcessHeap(), 0, This->decls);
343 HeapFree(GetProcessHeap(), 0, This);
347 /***********************************************************************
348 * DirectDrawCreate (DDRAW.@)
350 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
351 * interfaces in theory
353 * Arguments, return values: See DDRAW_Create
355 ***********************************************************************/
357 DirectDrawCreate(GUID *GUID,
361 TRACE("(%s,%p,%p)\n", debugstr_guid(GUID), DD, UnkOuter);
363 return DDRAW_Create(GUID, (void **) DD, UnkOuter, &IID_IDirectDraw);
366 /***********************************************************************
367 * DirectDrawCreateEx (DDRAW.@)
369 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
370 * interfaces are requested.
372 * Arguments, return values: See DDRAW_Create
374 ***********************************************************************/
376 DirectDrawCreateEx(GUID *GUID,
381 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(GUID), DD, debugstr_guid(iid), UnkOuter);
383 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
384 return DDERR_INVALIDPARAMS;
386 return DDRAW_Create(GUID, DD, UnkOuter, iid);
389 /***********************************************************************
390 * DirectDrawEnumerateA (DDRAW.@)
392 * Enumerates legacy ddraw drivers, ascii version. We only have one
393 * driver, which relays to WineD3D. If we were sufficiently cool,
394 * we could offer various interfaces, which use a different default surface
395 * implementation, but I think it's better to offer this choice in
396 * winecfg, because some apps use the default driver, so we would need
397 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
400 * Callback: Callback function from the app
401 * Context: Argument to the call back.
405 * E_INVALIDARG if the Callback caused a page fault
408 ***********************************************************************/
410 DirectDrawEnumerateA(LPDDENUMCALLBACKA Callback,
415 TRACE(" Enumerating default DirectDraw HAL interface\n");
416 /* We only have one driver */
419 static CHAR driver_desc[] = "DirectDraw HAL",
420 driver_name[] = "display";
422 stop = !Callback(NULL, driver_desc, driver_name, Context);
430 TRACE(" End of enumeration\n");
434 /***********************************************************************
435 * DirectDrawEnumerateExA (DDRAW.@)
437 * Enumerates DirectDraw7 drivers, ascii version. See
438 * the comments above DirectDrawEnumerateA for more details.
440 * The Flag member is not supported right now.
442 ***********************************************************************/
444 DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA Callback,
449 TRACE("Enumerating default DirectDraw HAL interface\n");
451 /* We only have one driver by now */
454 static CHAR driver_desc[] = "DirectDraw HAL",
455 driver_name[] = "display";
457 /* QuickTime expects the description "DirectDraw HAL" */
458 stop = !Callback(NULL, driver_desc, driver_name, Context, 0);
466 TRACE("End of enumeration\n");
470 /***********************************************************************
471 * DirectDrawEnumerateW (DDRAW.@)
473 * Enumerates legacy drivers, unicode version. See
474 * the comments above DirectDrawEnumerateA for more details.
476 * The Flag member is not supported right now.
478 ***********************************************************************/
480 /***********************************************************************
481 * DirectDrawEnumerateExW (DDRAW.@)
483 * Enumerates DirectDraw7 drivers, unicode version. See
484 * the comments above DirectDrawEnumerateA for more details.
486 * The Flag member is not supported right now.
488 ***********************************************************************/
490 /***********************************************************************
491 * Classfactory implementation.
492 ***********************************************************************/
494 /***********************************************************************
495 * CF_CreateDirectDraw
497 * DDraw creation function for the class factory
500 * UnkOuter: Set to NULL
501 * iid: ID of the wanted interface
502 * obj: Address to pass the interface pointer back
505 * DD_OK / DDERR*, see DDRAW_Create
507 ***********************************************************************/
509 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
514 TRACE("(%p,%s,%p)\n", UnkOuter, debugstr_guid(iid), obj);
516 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
520 /***********************************************************************
521 * CF_CreateDirectDraw
523 * Clipper creation function for the class factory
526 * UnkOuter: Set to NULL
527 * iid: ID of the wanted interface
528 * obj: Address to pass the interface pointer back
531 * DD_OK / DDERR*, see DDRAW_Create
533 ***********************************************************************/
535 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
539 IDirectDrawClipper *Clip;
541 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
542 if (hr != DD_OK) return hr;
544 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
545 IDirectDrawClipper_Release(Clip);
549 static const struct object_creation_info object_creation[] =
551 { &CLSID_DirectDraw, CF_CreateDirectDraw },
552 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
553 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
556 /*******************************************************************************
557 * IDirectDrawClassFactory::QueryInterface
559 * QueryInterface for the class factory
562 * riid Reference to identifier of queried interface
563 * ppv Address to return the interface pointer at
567 * Failure: E_NOINTERFACE
569 *******************************************************************************/
570 static HRESULT WINAPI
571 IDirectDrawClassFactoryImpl_QueryInterface(IClassFactory *iface,
575 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
577 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obj);
579 if (IsEqualGUID(riid, &IID_IUnknown)
580 || IsEqualGUID(riid, &IID_IClassFactory))
582 IClassFactory_AddRef(iface);
587 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),obj);
588 return E_NOINTERFACE;
591 /*******************************************************************************
592 * IDirectDrawClassFactory::AddRef
594 * AddRef for the class factory
599 *******************************************************************************/
601 IDirectDrawClassFactoryImpl_AddRef(IClassFactory *iface)
603 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
604 ULONG ref = InterlockedIncrement(&This->ref);
606 TRACE("(%p)->() incrementing from %d.\n", This, ref - 1);
611 /*******************************************************************************
612 * IDirectDrawClassFactory::Release
614 * Release for the class factory. If the refcount falls to 0, the object
620 *******************************************************************************/
622 IDirectDrawClassFactoryImpl_Release(IClassFactory *iface)
624 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
625 ULONG ref = InterlockedDecrement(&This->ref);
626 TRACE("(%p)->() decrementing from %d.\n", This, ref+1);
629 HeapFree(GetProcessHeap(), 0, This);
635 /*******************************************************************************
636 * IDirectDrawClassFactory::CreateInstance
638 * What is this? Seems to create DirectDraw objects...
641 * The ususal things???
646 *******************************************************************************/
647 static HRESULT WINAPI
648 IDirectDrawClassFactoryImpl_CreateInstance(IClassFactory *iface,
653 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
655 TRACE("(%p)->(%p,%s,%p)\n",This,UnkOuter,debugstr_guid(riid),obj);
657 return This->pfnCreateInstance(UnkOuter, riid, obj);
660 /*******************************************************************************
661 * IDirectDrawClassFactory::LockServer
669 * S_OK, because it's a stub
671 *******************************************************************************/
672 static HRESULT WINAPI
673 IDirectDrawClassFactoryImpl_LockServer(IClassFactory *iface,BOOL dolock)
675 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
676 FIXME("(%p)->(%d),stub!\n",This,dolock);
680 /*******************************************************************************
681 * The class factory VTable
682 *******************************************************************************/
683 static const IClassFactoryVtbl IClassFactory_Vtbl =
685 IDirectDrawClassFactoryImpl_QueryInterface,
686 IDirectDrawClassFactoryImpl_AddRef,
687 IDirectDrawClassFactoryImpl_Release,
688 IDirectDrawClassFactoryImpl_CreateInstance,
689 IDirectDrawClassFactoryImpl_LockServer
692 /*******************************************************************************
693 * DllGetClassObject [DDRAW.@]
694 * Retrieves class object from a DLL object
697 * Docs say returns STDAPI
700 * rclsid [I] CLSID for the class object
701 * riid [I] Reference to identifier of interface for class object
702 * ppv [O] Address of variable to receive interface pointer for riid
706 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
709 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
712 IClassFactoryImpl *factory;
714 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
716 if ( !IsEqualGUID( &IID_IClassFactory, riid )
717 && ! IsEqualGUID( &IID_IUnknown, riid) )
718 return E_NOINTERFACE;
720 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
722 if (IsEqualGUID(object_creation[i].clsid, rclsid))
726 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
728 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
729 return CLASS_E_CLASSNOTAVAILABLE;
732 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
733 if (factory == NULL) return E_OUTOFMEMORY;
735 ICOM_INIT_INTERFACE(factory, IClassFactory, IClassFactory_Vtbl);
738 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
740 *ppv = ICOM_INTERFACE(factory, IClassFactory);
745 /*******************************************************************************
746 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
752 HRESULT WINAPI DllCanUnloadNow(void)
754 FIXME("(void): stub\n");
758 /*******************************************************************************
761 * Callback function for the EnumSurfaces call in DllMain.
762 * Dumps some surface info and releases the surface
765 * surf: The enumerated surface
766 * desc: it's description
767 * context: Pointer to the ddraw impl
771 *******************************************************************************/
772 static HRESULT WINAPI
773 DestroyCallback(IDirectDrawSurface7 *surf,
774 DDSURFACEDESC2 *desc,
777 IDirectDrawSurfaceImpl *Impl = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surf);
778 IDirectDrawImpl *ddraw = (IDirectDrawImpl *) context;
781 ref = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
782 WARN("Surface %p has an reference count of %d\n", Impl, ref);
784 /* Skip surfaces which are attached somewhere or which are
785 * part of a complex compound. They will get released when destroying
788 if( (Impl->first_complex != Impl) || (Impl->first_attached != Impl) )
790 /* Skip our depth stencil surface, it will be released with the render target */
791 if( Impl == ddraw->DepthStencilBuffer)
794 /* Destroy the surface */
795 while(ref) ref = IDirectDrawSurface7_Release(surf);
800 /***********************************************************************
803 * Reads a config key from the registry. Taken from WineD3D
805 ***********************************************************************/
806 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
808 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
809 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
810 return ERROR_FILE_NOT_FOUND;
813 /***********************************************************************
816 * Could be used to register DirectDraw drivers, if we have more than
817 * one. Also used to destroy any objects left at unload if the
818 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
820 ***********************************************************************/
822 DllMain(HINSTANCE hInstDLL,
826 TRACE("(%p,%x,%p)\n", hInstDLL, Reason, lpv);
827 if (Reason == DLL_PROCESS_ATTACH)
829 char buffer[MAX_PATH+10];
830 DWORD size = sizeof(buffer);
835 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
836 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
838 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
839 if (len && len < MAX_PATH)
842 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
843 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
845 char *p, *appname = buffer;
846 if ((p = strrchr( appname, '/' ))) appname = p + 1;
847 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
848 strcat( appname, "\\Direct3D" );
849 TRACE("appname = [%s]\n", appname);
850 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
851 RegCloseKey( tmpkey );
855 if ( 0 != hkey || 0 != appkey )
857 if ( !get_config_key( hkey, appkey, "DirectDrawRenderer", buffer, size) )
859 if (!strcmp(buffer,"gdi"))
861 TRACE("Defaulting to GDI surfaces\n");
862 DefaultSurfaceType = SURFACE_GDI;
864 else if (!strcmp(buffer,"opengl"))
866 TRACE("Defaulting to opengl surfaces\n");
867 DefaultSurfaceType = SURFACE_OPENGL;
871 ERR("Unknown default surface type. Supported are:\n gdi, opengl\n");
876 DisableThreadLibraryCalls(hInstDLL);
878 else if (Reason == DLL_PROCESS_DETACH)
880 if(!list_empty(&global_ddraw_list))
882 struct list *entry, *entry2;
883 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
885 /* We remove elemets from this loop */
886 LIST_FOR_EACH_SAFE(entry, entry2, &global_ddraw_list)
891 IDirectDrawImpl *ddraw = LIST_ENTRY(entry, IDirectDrawImpl, ddraw_list_entry);
893 WARN("DDraw %p has a refcount of %d\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref3 + ddraw->ref2 + ddraw->ref1);
895 /* Add references to each interface to avoid freeing them unexpectadely */
896 IDirectDraw_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw));
897 IDirectDraw2_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw2));
898 IDirectDraw3_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw3));
899 IDirectDraw4_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw4));
900 IDirectDraw7_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw7));
902 /* Does a D3D device exist? Destroy it
903 * TODO: Destroy all Vertex buffers, Lights, Materials
904 * and execture buffers too
908 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
909 while(IDirect3DDevice7_Release(ICOM_INTERFACE(ddraw->d3ddevice, IDirect3DDevice7)));
912 /* Try to release the objects
913 * Do an EnumSurfaces to find any hanging surfaces
915 memset(&desc, 0, sizeof(desc));
916 desc.dwSize = sizeof(desc);
917 for(i = 0; i <= 1; i++)
919 hr = IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(ddraw, IDirectDraw7),
925 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
928 /* Check the surface count */
929 if(ddraw->surfaces > 0)
930 ERR("DDraw %p still has %d surfaces attached\n", ddraw, ddraw->surfaces);
932 /* Release all hanging references to destroy the objects. This
933 * restores the screen mode too
935 while(IDirectDraw_Release(ICOM_INTERFACE(ddraw, IDirectDraw)));
936 while(IDirectDraw2_Release(ICOM_INTERFACE(ddraw, IDirectDraw2)));
937 while(IDirectDraw3_Release(ICOM_INTERFACE(ddraw, IDirectDraw3)));
938 while(IDirectDraw4_Release(ICOM_INTERFACE(ddraw, IDirectDraw4)));
939 while(IDirectDraw7_Release(ICOM_INTERFACE(ddraw, IDirectDraw7)));
948 remove_ddraw_object(IDirectDrawImpl *ddraw)
950 EnterCriticalSection(&ddraw_list_cs);
951 list_remove(&ddraw->ddraw_list_entry);
952 LeaveCriticalSection(&ddraw_list_cs);