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 /***********************************************************************
63 * Helper function for DirectDrawCreate and friends
64 * Creates a new DDraw interface with the given REFIID
66 * Interfaces that can be created:
67 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
68 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
69 * IDirect3D interfaces?)
72 * guid: ID of the requested driver, NULL for the default driver.
73 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
74 * DD: Used to return the pointer to the created object
75 * UnkOuter: For aggregation, which is unsupported. Must be NULL
76 * iid: requested version ID.
79 * DD_OK if the Interface was created successfully
80 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
81 * E_OUTOFMEMORY if some allocation failed
83 ***********************************************************************/
85 DDRAW_Create(GUID *guid,
90 IDirectDrawImpl *This = NULL;
92 IWineD3D *wineD3D = NULL;
93 IWineD3DDevice *wineD3DDevice = NULL;
95 WINED3DDEVTYPE devicetype;
97 TRACE("(%s,%p,%p)\n", debugstr_guid(guid), DD, UnkOuter);
101 /* We don't care about this guids. Well, there's no special guid anyway
104 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
106 /* Use the reference device id. This doesn't actually change anything,
107 * WineD3D always uses OpenGL for D3D rendering. One could make it request
110 devicetype = WINED3DDEVTYPE_REF;
112 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
114 devicetype = WINED3DDEVTYPE_HAL;
121 /* DDraw doesn't support aggregation, according to msdn */
122 if (UnkOuter != NULL)
123 return CLASS_E_NOAGGREGATION;
125 /* DirectDraw creation comes here */
126 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawImpl));
129 ERR("Out of memory when creating DirectDraw\n");
130 return E_OUTOFMEMORY;
134 * IDirectDraw and IDirect3D are the same object,
135 * QueryInterface is used to get other interfaces.
137 ICOM_INIT_INTERFACE(This, IDirectDraw, IDirectDraw1_Vtbl);
138 ICOM_INIT_INTERFACE(This, IDirectDraw2, IDirectDraw2_Vtbl);
139 ICOM_INIT_INTERFACE(This, IDirectDraw4, IDirectDraw4_Vtbl);
140 ICOM_INIT_INTERFACE(This, IDirectDraw7, IDirectDraw7_Vtbl);
141 ICOM_INIT_INTERFACE(This, IDirect3D, IDirect3D1_Vtbl);
142 ICOM_INIT_INTERFACE(This, IDirect3D2, IDirect3D2_Vtbl);
143 ICOM_INIT_INTERFACE(This, IDirect3D3, IDirect3D3_Vtbl);
144 ICOM_INIT_INTERFACE(This, IDirect3D7, IDirect3D7_Vtbl);
146 /* See comments in IDirectDrawImpl_CreateNewSurface for a description
148 * Read from a registry key, should add a winecfg option later
150 This->ImplType = DefaultSurfaceType;
152 /* Get the current screen settings */
153 hDC = CreateDCA("DISPLAY", NULL, NULL, NULL);
154 This->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
156 This->orig_width = GetSystemMetrics(SM_CXSCREEN);
157 This->orig_height = GetSystemMetrics(SM_CYSCREEN);
159 if (hWineD3D == (HMODULE) -1)
161 hWineD3D = LoadLibraryA("wined3d");
163 pWineDirect3DCreate = (fnWineDirect3DCreate) GetProcAddress(hWineD3D, "WineDirect3DCreate");
168 ERR("Couldn't load WineD3D - OpenGL libs not present?\n");
169 hr = DDERR_NODIRECTDRAWSUPPORT;
173 /* Initialize WineD3D
175 * All Rendering (2D and 3D) is relayed to WineD3D,
176 * but DirectDraw specific management, like DDSURFACEDESC and DDPIXELFORMAT
177 * structure handling is handled in this lib.
179 wineD3D = pWineDirect3DCreate(0 /* SDKVersion */, 7 /* DXVersion */, (IUnknown *) This /* Parent */);
182 ERR("Failed to initialise WineD3D\n");
186 This->wineD3D = wineD3D;
187 TRACE("WineD3D created at %p\n", wineD3D);
189 /* Initialized member...
191 * It is set to false at creation time, and set to true in
192 * IDirectDraw7::Initialize. Its sole purpose is to return DD_OK on
193 * initialize only once
195 This->initialized = FALSE;
197 /* Initialize WineD3DDevice
199 * It is used for screen setup, surface and palette creation
200 * When a Direct3DDevice7 is created, the D3D capabilities of WineD3D are
203 hr = IWineD3D_CreateDevice(wineD3D,
204 0 /*D3D_ADAPTER_DEFAULT*/,
206 NULL, /* FocusWindow, don't know yet */
207 0, /* BehaviorFlags */
209 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7));
212 ERR("Failed to create a wineD3DDevice, result = %lx\n", hr);
215 This->wineD3DDevice = wineD3DDevice;
216 TRACE("wineD3DDevice created at %p\n", This->wineD3DDevice);
218 /* Register the window class
220 * It is used to create a hidden window for D3D
221 * rendering, if the application didn't pass one.
222 * It can also be used for Creating a device window
223 * from SetCooperativeLevel
225 * The name: DDRAW_<address>. The classname is
226 * 32 bit long, so a 64 bit address will fit nicely
227 * (Will this be compiled for 64 bit anyway?)
230 sprintf(This->classname, "DDRAW_%p", This);
232 memset(&This->wnd_class, 0, sizeof(This->wnd_class));
233 This->wnd_class.style = CS_HREDRAW | CS_VREDRAW;
234 This->wnd_class.lpfnWndProc = DefWindowProcA;
235 This->wnd_class.cbClsExtra = 0;
236 This->wnd_class.cbWndExtra = 0;
237 This->wnd_class.hInstance = GetModuleHandleA(0);
238 This->wnd_class.hIcon = 0;
239 This->wnd_class.hCursor = 0;
240 This->wnd_class.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
241 This->wnd_class.lpszMenuName = NULL;
242 This->wnd_class.lpszClassName = This->classname;
243 if(!RegisterClassA(&This->wnd_class))
245 ERR("RegisterClassA failed!\n");
249 /* Get the amount of video memory */
250 This->total_vidmem = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
252 /* Initialize the caps */
253 This->caps.dwSize = sizeof(This->caps);
254 /* do not report DDCAPS_OVERLAY and friends since we don't support overlays */
255 #define BLIT_CAPS (DDCAPS_BLT | DDCAPS_BLTCOLORFILL | DDCAPS_BLTDEPTHFILL \
256 | DDCAPS_BLTSTRETCH | DDCAPS_CANBLTSYSMEM | DDCAPS_CANCLIP \
257 | DDCAPS_CANCLIPSTRETCHED | DDCAPS_COLORKEY \
258 | DDCAPS_COLORKEYHWASSIST | DDCAPS_ALIGNBOUNDARYSRC )
259 #define CKEY_CAPS (DDCKEYCAPS_DESTBLT | DDCKEYCAPS_SRCBLT)
260 #define FX_CAPS (DDFXCAPS_BLTALPHA | DDFXCAPS_BLTMIRRORLEFTRIGHT \
261 | DDFXCAPS_BLTMIRRORUPDOWN | DDFXCAPS_BLTROTATION90 \
262 | DDFXCAPS_BLTSHRINKX | DDFXCAPS_BLTSHRINKXN \
263 | DDFXCAPS_BLTSHRINKY | DDFXCAPS_BLTSHRINKXN \
264 | DDFXCAPS_BLTSTRETCHX | DDFXCAPS_BLTSTRETCHXN \
265 | DDFXCAPS_BLTSTRETCHY | DDFXCAPS_BLTSTRETCHYN)
266 This->caps.dwCaps |= DDCAPS_GDI | DDCAPS_PALETTE | BLIT_CAPS;
268 This->caps.dwCaps2 |= DDCAPS2_CERTIFIED | DDCAPS2_NOPAGELOCKREQUIRED |
269 DDCAPS2_PRIMARYGAMMA | DDCAPS2_WIDESURFACES |
270 DDCAPS2_CANRENDERWINDOWED;
271 This->caps.dwCKeyCaps |= CKEY_CAPS;
272 This->caps.dwFXCaps |= FX_CAPS;
273 This->caps.dwPalCaps |= DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE;
274 This->caps.dwVidMemTotal = This->total_vidmem;
275 This->caps.dwVidMemFree = This->total_vidmem;
276 This->caps.dwSVBCaps |= BLIT_CAPS;
277 This->caps.dwSVBCKeyCaps |= CKEY_CAPS;
278 This->caps.dwSVBFXCaps |= FX_CAPS;
279 This->caps.dwVSBCaps |= BLIT_CAPS;
280 This->caps.dwVSBCKeyCaps |= CKEY_CAPS;
281 This->caps.dwVSBFXCaps |= FX_CAPS;
282 This->caps.dwSSBCaps |= BLIT_CAPS;
283 This->caps.dwSSBCKeyCaps |= CKEY_CAPS;
284 This->caps.dwSSBFXCaps |= FX_CAPS;
285 This->caps.ddsCaps.dwCaps |= DDSCAPS_ALPHA | DDSCAPS_BACKBUFFER |
286 DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER |
287 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_PALETTE |
288 DDSCAPS_PRIMARYSURFACE | DDSCAPS_SYSTEMMEMORY |
289 DDSCAPS_VIDEOMEMORY | DDSCAPS_VISIBLE;
290 /* Hacks for D3D code */
291 /* TODO: Check if WineD3D has 3D enabled
292 Need opengl surfaces or auto for 3D
294 if(This->ImplType == 0 || This->ImplType == SURFACE_OPENGL)
296 This->caps.dwCaps |= DDCAPS_3D;
297 This->caps.ddsCaps.dwCaps |= DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER;
299 This->caps.ddsOldCaps.dwCaps = This->caps.ddsCaps.dwCaps;
305 /* Add the object to the ddraw cleanup list */
306 This->next = ddraw_list;
309 /* Call QueryInterface to get the pointer to the requested interface. This also initializes
310 * The required refcount
312 hr = IDirectDraw7_QueryInterface( ICOM_INTERFACE(This, IDirectDraw7), iid, DD);
313 if(SUCCEEDED(hr)) return DD_OK;
316 /* Let's hope we never need this ;) */
317 if(wineD3DDevice) IWineD3DDevice_Release(wineD3DDevice);
318 if(wineD3D) IWineD3D_Release(wineD3D);
319 HeapFree(GetProcessHeap(), 0, This);
323 /***********************************************************************
324 * DirectDrawCreate (DDRAW.@)
326 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
327 * interfaces in theory
329 * Arguments, return values: See DDRAW_Create
331 ***********************************************************************/
333 DirectDrawCreate(GUID *GUID,
337 TRACE("(%s,%p,%p)\n", debugstr_guid(GUID), DD, UnkOuter);
339 return DDRAW_Create(GUID, (void **) DD, UnkOuter, &IID_IDirectDraw);
342 /***********************************************************************
343 * DirectDrawCreateEx (DDRAW.@)
345 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
346 * interfaces are requested.
348 * Arguments, return values: See DDRAW_Create
350 ***********************************************************************/
352 DirectDrawCreateEx(GUID *GUID,
357 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(GUID), DD, debugstr_guid(iid), UnkOuter);
359 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
360 return DDERR_INVALIDPARAMS;
362 return DDRAW_Create(GUID, DD, UnkOuter, iid);
365 /***********************************************************************
366 * DirectDrawEnumerateA (DDRAW.@)
368 * Enumerates legacy ddraw drivers, ascii version. We only have one
369 * driver, which relays to WineD3D. If we were sufficiently cool,
370 * we could offer various interfaces, which use a different default surface
371 * implementation, but I think it's better to offer this choice in
372 * winecfg, because some apps use the default driver, so we would need
373 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
376 * Callback: Callback function from the app
377 * Context: Argument to the call back.
381 * E_INVALIDARG if the Callback caused a page fault
384 ***********************************************************************/
386 DirectDrawEnumerateA(LPDDENUMCALLBACKA Callback,
391 TRACE(" Enumerating default DirectDraw HAL interface\n");
392 /* We only have one driver */
395 static CHAR driver_desc[] = "DirectDraw HAL",
396 driver_name[] = "display";
398 stop = !Callback(NULL, driver_desc, driver_name, Context);
406 TRACE(" End of enumeration\n");
410 /***********************************************************************
411 * DirectDrawEnumerateExA (DDRAW.@)
413 * Enumerates DirectDraw7 drivers, ascii version. See
414 * the comments above DirectDrawEnumerateA for more details.
416 * The Flag member is not supported right now.
418 ***********************************************************************/
420 DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA Callback,
425 TRACE("Enumerating default DirectDraw HAL interface\n");
427 /* We only have one driver by now */
430 static CHAR driver_desc[] = "DirectDraw HAL",
431 driver_name[] = "display";
433 /* QuickTime expects the description "DirectDraw HAL" */
434 stop = !Callback(NULL, driver_desc, driver_name, Context, 0);
442 TRACE("End of enumeration\n");
446 /***********************************************************************
447 * DirectDrawEnumerateW (DDRAW.@)
449 * Enumerates legacy drivers, unicode version. See
450 * the comments above DirectDrawEnumerateA for more details.
452 * The Flag member is not supported right now.
454 ***********************************************************************/
456 /***********************************************************************
457 * DirectDrawEnumerateExW (DDRAW.@)
459 * Enumerates DirectDraw7 drivers, unicode version. See
460 * the comments above DirectDrawEnumerateA for more details.
462 * The Flag member is not supported right now.
464 ***********************************************************************/
466 /***********************************************************************
467 * Classfactory implementation.
468 ***********************************************************************/
470 /***********************************************************************
471 * CF_CreateDirectDraw
473 * DDraw creation function for the class factory
476 * UnkOuter: Set to NULL
477 * iid: ID of the wanted interface
478 * obj: Address to pass the interface pointer back
481 * DD_OK / DDERR*, see DDRAW_Create
483 ***********************************************************************/
485 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
490 TRACE("(%p,%s,%p)\n", UnkOuter, debugstr_guid(iid), obj);
492 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
496 /***********************************************************************
497 * CF_CreateDirectDraw
499 * Clipper creation function for the class factory
502 * UnkOuter: Set to NULL
503 * iid: ID of the wanted interface
504 * obj: Address to pass the interface pointer back
507 * DD_OK / DDERR*, see DDRAW_Create
509 ***********************************************************************/
511 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
515 IDirectDrawClipper *Clip;
517 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
518 if (hr != DD_OK) return hr;
520 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
521 IDirectDrawClipper_Release(Clip);
525 static const struct object_creation_info object_creation[] =
527 { &CLSID_DirectDraw, CF_CreateDirectDraw },
528 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
529 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
532 /*******************************************************************************
533 * IDirectDrawClassFactory::QueryInterface
535 * QueryInterface for the class factory
538 * riid Reference to identifier of queried interface
539 * ppv Address to return the interface pointer at
543 * Failure: E_NOINTERFACE
545 *******************************************************************************/
546 static HRESULT WINAPI
547 IDirectDrawClassFactoryImpl_QueryInterface(IClassFactory *iface,
551 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
553 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obj);
555 if (IsEqualGUID(riid, &IID_IUnknown)
556 || IsEqualGUID(riid, &IID_IClassFactory))
558 IClassFactory_AddRef(iface);
563 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),obj);
564 return E_NOINTERFACE;
567 /*******************************************************************************
568 * IDirectDrawClassFactory::AddRef
570 * AddRef for the class factory
575 *******************************************************************************/
577 IDirectDrawClassFactoryImpl_AddRef(IClassFactory *iface)
579 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
580 ULONG ref = InterlockedIncrement(&This->ref);
582 TRACE("(%p)->() incrementing from %ld.\n", This, ref - 1);
587 /*******************************************************************************
588 * IDirectDrawClassFactory::Release
590 * Release for the class factory. If the refcount falls to 0, the object
596 *******************************************************************************/
598 IDirectDrawClassFactoryImpl_Release(IClassFactory *iface)
600 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
601 ULONG ref = InterlockedDecrement(&This->ref);
602 TRACE("(%p)->() decrementing from %ld.\n", This, ref+1);
605 HeapFree(GetProcessHeap(), 0, This);
611 /*******************************************************************************
612 * IDirectDrawClassFactory::CreateInstance
614 * What is this? Seems to create DirectDraw objects...
617 * The ususal things???
622 *******************************************************************************/
623 static HRESULT WINAPI
624 IDirectDrawClassFactoryImpl_CreateInstance(IClassFactory *iface,
629 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
631 TRACE("(%p)->(%p,%s,%p)\n",This,UnkOuter,debugstr_guid(riid),obj);
633 return This->pfnCreateInstance(UnkOuter, riid, obj);
636 /*******************************************************************************
637 * IDirectDrawClassFactory::LockServer
645 * S_OK, because it's a stub
647 *******************************************************************************/
648 static HRESULT WINAPI
649 IDirectDrawClassFactoryImpl_LockServer(IClassFactory *iface,BOOL dolock)
651 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
652 FIXME("(%p)->(%d),stub!\n",This,dolock);
656 /*******************************************************************************
657 * The class factory VTable
658 *******************************************************************************/
659 static const IClassFactoryVtbl IClassFactory_Vtbl =
661 IDirectDrawClassFactoryImpl_QueryInterface,
662 IDirectDrawClassFactoryImpl_AddRef,
663 IDirectDrawClassFactoryImpl_Release,
664 IDirectDrawClassFactoryImpl_CreateInstance,
665 IDirectDrawClassFactoryImpl_LockServer
668 /*******************************************************************************
669 * DllGetClassObject [DDRAW.@]
670 * Retrieves class object from a DLL object
673 * Docs say returns STDAPI
676 * rclsid [I] CLSID for the class object
677 * riid [I] Reference to identifier of interface for class object
678 * ppv [O] Address of variable to receive interface pointer for riid
682 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
685 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
688 IClassFactoryImpl *factory;
690 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
692 if ( !IsEqualGUID( &IID_IClassFactory, riid )
693 && ! IsEqualGUID( &IID_IUnknown, riid) )
694 return E_NOINTERFACE;
696 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
698 if (IsEqualGUID(object_creation[i].clsid, rclsid))
702 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
704 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
705 return CLASS_E_CLASSNOTAVAILABLE;
708 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
709 if (factory == NULL) return E_OUTOFMEMORY;
711 ICOM_INIT_INTERFACE(factory, IClassFactory, IClassFactory_Vtbl);
714 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
716 *ppv = ICOM_INTERFACE(factory, IClassFactory);
721 /*******************************************************************************
722 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
728 HRESULT WINAPI DllCanUnloadNow(void)
730 FIXME("(void): stub\n");
734 /*******************************************************************************
737 * Callback function for the EnumSurfaces call in DllMain.
738 * Dumps some surface info and releases the surface
741 * surf: The enumerated surface
742 * desc: it's description
743 * context: Pointer to the ddraw impl
747 *******************************************************************************/
748 static HRESULT WINAPI
749 DestroyCallback(IDirectDrawSurface7 *surf,
750 DDSURFACEDESC2 *desc,
753 IDirectDrawSurfaceImpl *Impl = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surf);
754 IDirectDrawImpl *ddraw = (IDirectDrawImpl *) context;
757 ref = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
758 WARN("Surface %p has an reference count of %ld\n", Impl, ref);
760 /* Skip surfaces which are attached somewhere or which are
761 * part of a complex compound. They will get released when destroying
764 if( (Impl->first_complex != Impl) || (Impl->first_attached != Impl) )
766 /* Skip our depth stencil surface, it will be released with the render target */
767 if( Impl == ddraw->DepthStencilBuffer)
770 /* Destroy the surface */
771 while(ref) ref = IDirectDrawSurface7_Release(surf);
776 /***********************************************************************
779 * Reads a config key from the registry. Taken from WineD3D
781 ***********************************************************************/
782 inline static DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
784 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
785 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
786 return ERROR_FILE_NOT_FOUND;
789 /***********************************************************************
792 * Could be used to register DirectDraw drivers, if we have more than
793 * one. Also used to destroy any objects left at unload if the
794 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
796 ***********************************************************************/
798 DllMain(HINSTANCE hInstDLL,
802 static LONG counter = 0;
804 TRACE("(%p,%lx,%p)\n", hInstDLL, Reason, lpv);
805 if (Reason == DLL_PROCESS_ATTACH)
807 char buffer[MAX_PATH+10];
808 DWORD size = sizeof(buffer);
813 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
814 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
816 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
817 if (len && len < MAX_PATH)
820 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
821 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
823 char *p, *appname = buffer;
824 if ((p = strrchr( appname, '/' ))) appname = p + 1;
825 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
826 strcat( appname, "\\Direct3D" );
827 TRACE("appname = [%s]\n", appname);
828 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
829 RegCloseKey( tmpkey );
833 if ( 0 != hkey || 0 != appkey )
835 if ( !get_config_key( hkey, appkey, "DirectDrawRenderer", buffer, size) )
837 if (!strcmp(buffer,"gdi"))
839 TRACE("Defaulting to GDI surfaces\n");
840 DefaultSurfaceType = SURFACE_GDI;
842 else if (!strcmp(buffer,"opengl"))
844 TRACE("Defaulting to opengl surfaces\n");
845 DefaultSurfaceType = SURFACE_OPENGL;
849 ERR("Unknown default surface type. Supported are:\n gdi, opengl");
854 DisableThreadLibraryCalls(hInstDLL);
855 TRACE("Attach counter: %ld\n", InterlockedIncrement(&counter));
857 else if (Reason == DLL_PROCESS_DETACH)
859 TRACE("Attach counter: %ld\n", InterlockedDecrement(&counter));
865 IDirectDrawImpl *ddraw;
866 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
868 for(ddraw = ddraw_list; ddraw; ddraw = ddraw->next)
874 WARN("DDraw %p has a refcount of %ld\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref2 + ddraw->ref1);
876 /* Add references to each interface to avoid freeing them unexpectadely */
877 IDirectDraw_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw));
878 IDirectDraw2_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw2));
879 IDirectDraw4_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw4));
880 IDirectDraw7_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw7));
882 /* Does a D3D device exist? Destroy it
883 * TODO: Destroy all Vertex buffers, Lights, Materials
884 * and execture buffers too
888 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
889 while(IDirect3DDevice7_Release(ICOM_INTERFACE(ddraw->d3ddevice, IDirect3DDevice7)));
892 /* Try to release the objects
893 * Do an EnumSurfaces to find any hanging surfaces
895 memset(&desc, 0, sizeof(desc));
896 desc.dwSize = sizeof(desc);
897 for(i = 0; i <= 1; i++)
899 hr = IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(ddraw, IDirectDraw7),
905 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
908 /* Check the surface count */
909 if(ddraw->surfaces > 0)
910 ERR("DDraw %p still has %ld surfaces attached\n", ddraw, ddraw->surfaces);
912 /* Release all hanging references to destroy the objects. This
913 * restores the screen mode too
915 while(IDirectDraw_Release(ICOM_INTERFACE(ddraw, IDirectDraw)));
916 while(IDirectDraw2_Release(ICOM_INTERFACE(ddraw, IDirectDraw2)));
917 while(IDirectDraw4_Release(ICOM_INTERFACE(ddraw, IDirectDraw4)));
918 while(IDirectDraw7_Release(ICOM_INTERFACE(ddraw, IDirectDraw7)));