wined3d: read_from_framebuffer_texture() isn't suitable for readback of onscreen...
[wine/testsucceed.git] / dlls / ddraw / main.c
blob45a156f171a90e38338b0ba1d1c01a4b7a16b68d
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
7 * Copyright 2008 Denver Gingerich
9 * This file contains the (internal) driver registration functions,
10 * driver enumeration APIs and DirectDraw creation functions.
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
29 #include "wine/debug.h"
31 #include <assert.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <stdlib.h>
36 #define COBJMACROS
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winerror.h"
41 #include "wingdi.h"
42 #include "wine/exception.h"
43 #include "winreg.h"
45 #include "ddraw.h"
46 #include "d3d.h"
48 #define DDRAW_INIT_GUID
49 #include "ddraw_private.h"
51 static typeof(WineDirect3DCreate) *pWineDirect3DCreate;
53 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
55 /* The configured default surface */
56 WINED3DSURFTYPE DefaultSurfaceType = SURFACE_UNKNOWN;
58 /* DDraw list and critical section */
59 static struct list global_ddraw_list = LIST_INIT(global_ddraw_list);
61 static CRITICAL_SECTION_DEBUG ddraw_cs_debug =
63 0, 0, &ddraw_cs,
64 { &ddraw_cs_debug.ProcessLocksList,
65 &ddraw_cs_debug.ProcessLocksList },
66 0, 0, { (DWORD_PTR)(__FILE__ ": ddraw_cs") }
68 CRITICAL_SECTION ddraw_cs = { &ddraw_cs_debug, -1, 0, 0, 0, 0 };
70 /* value of ForceRefreshRate */
71 DWORD force_refresh_rate = 0;
74 * Helper Function for DDRAW_Create and DirectDrawCreateClipper for
75 * lazy loading of the Wine D3D driver.
77 * Returns
78 * TRUE on success
79 * FALSE on failure.
82 BOOL LoadWineD3D(void)
84 static HMODULE hWineD3D = (HMODULE) -1;
85 if (hWineD3D == (HMODULE) -1)
87 hWineD3D = LoadLibraryA("wined3d");
88 if (hWineD3D)
90 pWineDirect3DCreate = (typeof(WineDirect3DCreate) *)GetProcAddress(hWineD3D, "WineDirect3DCreate");
91 pWineDirect3DCreateClipper = (typeof(WineDirect3DCreateClipper) *) GetProcAddress(hWineD3D, "WineDirect3DCreateClipper");
92 return TRUE;
95 return hWineD3D != NULL;
98 /***********************************************************************
100 * Helper function for DirectDrawCreate and friends
101 * Creates a new DDraw interface with the given REFIID
103 * Interfaces that can be created:
104 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
105 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
106 * IDirect3D interfaces?)
108 * Arguments:
109 * guid: ID of the requested driver, NULL for the default driver.
110 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
111 * DD: Used to return the pointer to the created object
112 * UnkOuter: For aggregation, which is unsupported. Must be NULL
113 * iid: requested version ID.
115 * Returns:
116 * DD_OK if the Interface was created successfully
117 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
118 * E_OUTOFMEMORY if some allocation failed
120 ***********************************************************************/
121 static HRESULT
122 DDRAW_Create(const GUID *guid,
123 void **DD,
124 IUnknown *UnkOuter,
125 REFIID iid)
127 IDirectDrawImpl *This = NULL;
128 HRESULT hr;
129 IWineD3D *wineD3D = NULL;
130 IWineD3DDevice *wineD3DDevice = NULL;
131 HDC hDC;
132 WINED3DDEVTYPE devicetype;
134 TRACE("(%s,%p,%p)\n", debugstr_guid(guid), DD, UnkOuter);
136 *DD = NULL;
138 /* We don't care about this guids. Well, there's no special guid anyway
139 * OK, we could
141 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
143 /* Use the reference device id. This doesn't actually change anything,
144 * WineD3D always uses OpenGL for D3D rendering. One could make it request
145 * indirect rendering
147 devicetype = WINED3DDEVTYPE_REF;
149 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
151 devicetype = WINED3DDEVTYPE_HAL;
153 else
155 devicetype = 0;
158 /* DDraw doesn't support aggregation, according to msdn */
159 if (UnkOuter != NULL)
160 return CLASS_E_NOAGGREGATION;
162 /* DirectDraw creation comes here */
163 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawImpl));
164 if(!This)
166 ERR("Out of memory when creating DirectDraw\n");
167 return E_OUTOFMEMORY;
170 /* The interfaces:
171 * IDirectDraw and IDirect3D are the same object,
172 * QueryInterface is used to get other interfaces.
174 This->lpVtbl = &IDirectDraw7_Vtbl;
175 This->IDirectDraw_vtbl = &IDirectDraw1_Vtbl;
176 This->IDirectDraw2_vtbl = &IDirectDraw2_Vtbl;
177 This->IDirectDraw3_vtbl = &IDirectDraw3_Vtbl;
178 This->IDirectDraw4_vtbl = &IDirectDraw4_Vtbl;
179 This->IDirect3D_vtbl = &IDirect3D1_Vtbl;
180 This->IDirect3D2_vtbl = &IDirect3D2_Vtbl;
181 This->IDirect3D3_vtbl = &IDirect3D3_Vtbl;
182 This->IDirect3D7_vtbl = &IDirect3D7_Vtbl;
183 This->device_parent_vtbl = &ddraw_wined3d_device_parent_vtbl;
185 /* See comments in IDirectDrawImpl_CreateNewSurface for a description
186 * of this member.
187 * Read from a registry key, should add a winecfg option later
189 This->ImplType = DefaultSurfaceType;
191 /* Get the current screen settings */
192 hDC = GetDC(0);
193 This->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
194 ReleaseDC(0, hDC);
195 This->orig_width = GetSystemMetrics(SM_CXSCREEN);
196 This->orig_height = GetSystemMetrics(SM_CYSCREEN);
198 if (!LoadWineD3D())
200 ERR("Couldn't load WineD3D - OpenGL libs not present?\n");
201 hr = DDERR_NODIRECTDRAWSUPPORT;
202 goto err_out;
205 /* Initialize WineD3D
207 * All Rendering (2D and 3D) is relayed to WineD3D,
208 * but DirectDraw specific management, like DDSURFACEDESC and DDPIXELFORMAT
209 * structure handling is handled in this lib.
211 wineD3D = pWineDirect3DCreate(7 /* DXVersion */, (IUnknown *) This /* Parent */);
212 if(!wineD3D)
214 ERR("Failed to initialise WineD3D\n");
215 hr = E_OUTOFMEMORY;
216 goto err_out;
218 This->wineD3D = wineD3D;
219 TRACE("WineD3D created at %p\n", wineD3D);
221 /* Initialized member...
223 * It is set to false at creation time, and set to true in
224 * IDirectDraw7::Initialize. Its sole purpose is to return DD_OK on
225 * initialize only once
227 This->initialized = FALSE;
229 /* Initialize WineD3DDevice
231 * It is used for screen setup, surface and palette creation
232 * When a Direct3DDevice7 is created, the D3D capabilities of WineD3D are
233 * initialized
235 hr = IWineD3D_CreateDevice(wineD3D, 0 /* D3D_ADAPTER_DEFAULT */, devicetype, NULL /* FocusWindow, don't know yet */,
236 0 /* BehaviorFlags */, (IUnknown *)This, (IWineD3DDeviceParent *)&This->device_parent_vtbl, &wineD3DDevice);
237 if(FAILED(hr))
239 ERR("Failed to create a wineD3DDevice, result = %x\n", hr);
240 goto err_out;
242 This->wineD3DDevice = wineD3DDevice;
243 TRACE("wineD3DDevice created at %p\n", This->wineD3DDevice);
245 /* Get the amount of video memory */
246 This->total_vidmem = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
248 list_init(&This->surface_list);
249 list_add_head(&global_ddraw_list, &This->ddraw_list_entry);
251 /* Call QueryInterface to get the pointer to the requested interface. This also initializes
252 * The required refcount
254 hr = IDirectDraw7_QueryInterface((IDirectDraw7 *)This, iid, DD);
255 if(SUCCEEDED(hr)) return DD_OK;
257 err_out:
258 /* Let's hope we never need this ;) */
259 if(wineD3DDevice) IWineD3DDevice_Release(wineD3DDevice);
260 if(wineD3D) IWineD3D_Release(wineD3D);
261 HeapFree(GetProcessHeap(), 0, This->decls);
262 HeapFree(GetProcessHeap(), 0, This);
263 return hr;
266 /***********************************************************************
267 * DirectDrawCreate (DDRAW.@)
269 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
270 * interfaces in theory
272 * Arguments, return values: See DDRAW_Create
274 ***********************************************************************/
275 HRESULT WINAPI DECLSPEC_HOTPATCH
276 DirectDrawCreate(GUID *GUID,
277 LPDIRECTDRAW *DD,
278 IUnknown *UnkOuter)
280 HRESULT hr;
281 TRACE("(%s,%p,%p)\n", debugstr_guid(GUID), DD, UnkOuter);
283 EnterCriticalSection(&ddraw_cs);
284 hr = DDRAW_Create(GUID, (void **) DD, UnkOuter, &IID_IDirectDraw);
285 LeaveCriticalSection(&ddraw_cs);
286 return hr;
289 /***********************************************************************
290 * DirectDrawCreateEx (DDRAW.@)
292 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
293 * interfaces are requested.
295 * Arguments, return values: See DDRAW_Create
297 ***********************************************************************/
298 HRESULT WINAPI DECLSPEC_HOTPATCH
299 DirectDrawCreateEx(GUID *GUID,
300 LPVOID *DD,
301 REFIID iid,
302 IUnknown *UnkOuter)
304 HRESULT hr;
305 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(GUID), DD, debugstr_guid(iid), UnkOuter);
307 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
308 return DDERR_INVALIDPARAMS;
310 EnterCriticalSection(&ddraw_cs);
311 hr = DDRAW_Create(GUID, DD, UnkOuter, iid);
312 LeaveCriticalSection(&ddraw_cs);
313 return hr;
316 /***********************************************************************
317 * DirectDrawEnumerateA (DDRAW.@)
319 * Enumerates legacy ddraw drivers, ascii version. We only have one
320 * driver, which relays to WineD3D. If we were sufficiently cool,
321 * we could offer various interfaces, which use a different default surface
322 * implementation, but I think it's better to offer this choice in
323 * winecfg, because some apps use the default driver, so we would need
324 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
326 * Arguments:
327 * Callback: Callback function from the app
328 * Context: Argument to the call back.
330 * Returns:
331 * DD_OK on success
332 * E_INVALIDARG if the Callback caused a page fault
335 ***********************************************************************/
336 HRESULT WINAPI
337 DirectDrawEnumerateA(LPDDENUMCALLBACKA Callback,
338 LPVOID Context)
340 TRACE("(%p, %p)\n", Callback, Context);
342 TRACE(" Enumerating default DirectDraw HAL interface\n");
343 /* We only have one driver */
344 __TRY
346 static CHAR driver_desc[] = "DirectDraw HAL",
347 driver_name[] = "display";
349 Callback(NULL, driver_desc, driver_name, Context);
351 __EXCEPT_PAGE_FAULT
353 return DDERR_INVALIDPARAMS;
355 __ENDTRY
357 TRACE(" End of enumeration\n");
358 return DD_OK;
361 /***********************************************************************
362 * DirectDrawEnumerateExA (DDRAW.@)
364 * Enumerates DirectDraw7 drivers, ascii version. See
365 * the comments above DirectDrawEnumerateA for more details.
367 * The Flag member is not supported right now.
369 ***********************************************************************/
370 HRESULT WINAPI
371 DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA Callback,
372 LPVOID Context,
373 DWORD Flags)
375 TRACE("(%p, %p, 0x%08x)\n", Callback, Context, Flags);
377 if (Flags & ~(DDENUM_ATTACHEDSECONDARYDEVICES |
378 DDENUM_DETACHEDSECONDARYDEVICES |
379 DDENUM_NONDISPLAYDEVICES))
380 return DDERR_INVALIDPARAMS;
382 if (Flags)
383 FIXME("flags 0x%08x not handled\n", Flags);
385 TRACE("Enumerating default DirectDraw HAL interface\n");
387 /* We only have one driver by now */
388 __TRY
390 static CHAR driver_desc[] = "DirectDraw HAL",
391 driver_name[] = "display";
393 /* QuickTime expects the description "DirectDraw HAL" */
394 Callback(NULL, driver_desc, driver_name, Context, 0);
396 __EXCEPT_PAGE_FAULT
398 return DDERR_INVALIDPARAMS;
400 __ENDTRY;
402 TRACE("End of enumeration\n");
403 return DD_OK;
406 /***********************************************************************
407 * DirectDrawEnumerateW (DDRAW.@)
409 * Enumerates legacy drivers, unicode version.
410 * This function is not implemented on Windows.
412 ***********************************************************************/
413 HRESULT WINAPI
414 DirectDrawEnumerateW(LPDDENUMCALLBACKW Callback,
415 LPVOID Context)
417 TRACE("(%p, %p)\n", Callback, Context);
419 if (!Callback)
420 return DDERR_INVALIDPARAMS;
421 else
422 return DDERR_UNSUPPORTED;
425 /***********************************************************************
426 * DirectDrawEnumerateExW (DDRAW.@)
428 * Enumerates DirectDraw7 drivers, unicode version.
429 * This function is not implemented on Windows.
431 ***********************************************************************/
432 HRESULT WINAPI
433 DirectDrawEnumerateExW(LPDDENUMCALLBACKEXW Callback,
434 LPVOID Context,
435 DWORD Flags)
437 TRACE("(%p, %p, 0x%x)\n", Callback, Context, Flags);
439 return DDERR_UNSUPPORTED;
442 /***********************************************************************
443 * Classfactory implementation.
444 ***********************************************************************/
446 /***********************************************************************
447 * CF_CreateDirectDraw
449 * DDraw creation function for the class factory
451 * Params:
452 * UnkOuter: Set to NULL
453 * iid: ID of the wanted interface
454 * obj: Address to pass the interface pointer back
456 * Returns
457 * DD_OK / DDERR*, see DDRAW_Create
459 ***********************************************************************/
460 static HRESULT
461 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
462 void **obj)
464 HRESULT hr;
466 TRACE("(%p,%s,%p)\n", UnkOuter, debugstr_guid(iid), obj);
468 EnterCriticalSection(&ddraw_cs);
469 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
470 LeaveCriticalSection(&ddraw_cs);
471 return hr;
474 /***********************************************************************
475 * CF_CreateDirectDraw
477 * Clipper creation function for the class factory
479 * Params:
480 * UnkOuter: Set to NULL
481 * iid: ID of the wanted interface
482 * obj: Address to pass the interface pointer back
484 * Returns
485 * DD_OK / DDERR*, see DDRAW_Create
487 ***********************************************************************/
488 static HRESULT
489 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
490 void **obj)
492 HRESULT hr;
493 IDirectDrawClipper *Clip;
495 EnterCriticalSection(&ddraw_cs);
496 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
497 if (hr != DD_OK)
499 LeaveCriticalSection(&ddraw_cs);
500 return hr;
503 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
504 IDirectDrawClipper_Release(Clip);
506 LeaveCriticalSection(&ddraw_cs);
507 return hr;
510 static const struct object_creation_info object_creation[] =
512 { &CLSID_DirectDraw, CF_CreateDirectDraw },
513 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
514 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
517 /*******************************************************************************
518 * IDirectDrawClassFactory::QueryInterface
520 * QueryInterface for the class factory
522 * PARAMS
523 * riid Reference to identifier of queried interface
524 * ppv Address to return the interface pointer at
526 * RETURNS
527 * Success: S_OK
528 * Failure: E_NOINTERFACE
530 *******************************************************************************/
531 static HRESULT WINAPI
532 IDirectDrawClassFactoryImpl_QueryInterface(IClassFactory *iface,
533 REFIID riid,
534 void **obj)
536 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
538 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obj);
540 if (IsEqualGUID(riid, &IID_IUnknown)
541 || IsEqualGUID(riid, &IID_IClassFactory))
543 IClassFactory_AddRef(iface);
544 *obj = This;
545 return S_OK;
548 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),obj);
549 return E_NOINTERFACE;
552 /*******************************************************************************
553 * IDirectDrawClassFactory::AddRef
555 * AddRef for the class factory
557 * RETURNS
558 * The new refcount
560 *******************************************************************************/
561 static ULONG WINAPI
562 IDirectDrawClassFactoryImpl_AddRef(IClassFactory *iface)
564 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
565 ULONG ref = InterlockedIncrement(&This->ref);
567 TRACE("(%p)->() incrementing from %d.\n", This, ref - 1);
569 return ref;
572 /*******************************************************************************
573 * IDirectDrawClassFactory::Release
575 * Release for the class factory. If the refcount falls to 0, the object
576 * is destroyed
578 * RETURNS
579 * The new refcount
581 *******************************************************************************/
582 static ULONG WINAPI
583 IDirectDrawClassFactoryImpl_Release(IClassFactory *iface)
585 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
586 ULONG ref = InterlockedDecrement(&This->ref);
587 TRACE("(%p)->() decrementing from %d.\n", This, ref+1);
589 if (ref == 0)
590 HeapFree(GetProcessHeap(), 0, This);
592 return ref;
596 /*******************************************************************************
597 * IDirectDrawClassFactory::CreateInstance
599 * What is this? Seems to create DirectDraw objects...
601 * Params
602 * The usual things???
604 * RETURNS
605 * ???
607 *******************************************************************************/
608 static HRESULT WINAPI
609 IDirectDrawClassFactoryImpl_CreateInstance(IClassFactory *iface,
610 IUnknown *UnkOuter,
611 REFIID riid,
612 void **obj)
614 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
616 TRACE("(%p)->(%p,%s,%p)\n",This,UnkOuter,debugstr_guid(riid),obj);
618 return This->pfnCreateInstance(UnkOuter, riid, obj);
621 /*******************************************************************************
622 * IDirectDrawClassFactory::LockServer
624 * What is this?
626 * Params
627 * ???
629 * RETURNS
630 * S_OK, because it's a stub
632 *******************************************************************************/
633 static HRESULT WINAPI
634 IDirectDrawClassFactoryImpl_LockServer(IClassFactory *iface,BOOL dolock)
636 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
637 FIXME("(%p)->(%d),stub!\n",This,dolock);
638 return S_OK;
641 /*******************************************************************************
642 * The class factory VTable
643 *******************************************************************************/
644 static const IClassFactoryVtbl IClassFactory_Vtbl =
646 IDirectDrawClassFactoryImpl_QueryInterface,
647 IDirectDrawClassFactoryImpl_AddRef,
648 IDirectDrawClassFactoryImpl_Release,
649 IDirectDrawClassFactoryImpl_CreateInstance,
650 IDirectDrawClassFactoryImpl_LockServer
653 /*******************************************************************************
654 * DllGetClassObject [DDRAW.@]
655 * Retrieves class object from a DLL object
657 * NOTES
658 * Docs say returns STDAPI
660 * PARAMS
661 * rclsid [I] CLSID for the class object
662 * riid [I] Reference to identifier of interface for class object
663 * ppv [O] Address of variable to receive interface pointer for riid
665 * RETURNS
666 * Success: S_OK
667 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
668 * E_UNEXPECTED
670 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
672 unsigned int i;
673 IClassFactoryImpl *factory;
675 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
677 if ( !IsEqualGUID( &IID_IClassFactory, riid )
678 && ! IsEqualGUID( &IID_IUnknown, riid) )
679 return E_NOINTERFACE;
681 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
683 if (IsEqualGUID(object_creation[i].clsid, rclsid))
684 break;
687 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
689 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
690 return CLASS_E_CLASSNOTAVAILABLE;
693 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
694 if (factory == NULL) return E_OUTOFMEMORY;
696 factory->lpVtbl = &IClassFactory_Vtbl;
697 factory->ref = 1;
699 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
701 *ppv = factory;
702 return S_OK;
706 /*******************************************************************************
707 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
709 * RETURNS
710 * Success: S_OK
711 * Failure: S_FALSE
713 HRESULT WINAPI DllCanUnloadNow(void)
715 return S_FALSE;
718 /*******************************************************************************
719 * DestroyCallback
721 * Callback function for the EnumSurfaces call in DllMain.
722 * Dumps some surface info and releases the surface
724 * Params:
725 * surf: The enumerated surface
726 * desc: it's description
727 * context: Pointer to the ddraw impl
729 * Returns:
730 * DDENUMRET_OK;
731 *******************************************************************************/
732 static HRESULT WINAPI
733 DestroyCallback(IDirectDrawSurface7 *surf,
734 DDSURFACEDESC2 *desc,
735 void *context)
737 IDirectDrawSurfaceImpl *Impl = (IDirectDrawSurfaceImpl *)surf;
738 ULONG ref;
740 ref = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
741 WARN("Surface %p has an reference count of %d\n", Impl, ref);
743 /* Skip surfaces which are attached somewhere or which are
744 * part of a complex compound. They will get released when destroying
745 * the root
747 if( (!Impl->is_complex_root) || (Impl->first_attached != Impl) )
748 return DDENUMRET_OK;
750 /* Destroy the surface */
751 while(ref) ref = IDirectDrawSurface7_Release(surf);
753 return DDENUMRET_OK;
756 /***********************************************************************
757 * get_config_key
759 * Reads a config key from the registry. Taken from WineD3D
761 ***********************************************************************/
762 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
764 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
765 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
766 return ERROR_FILE_NOT_FOUND;
769 /***********************************************************************
770 * DllMain (DDRAW.0)
772 * Could be used to register DirectDraw drivers, if we have more than
773 * one. Also used to destroy any objects left at unload if the
774 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
776 ***********************************************************************/
777 BOOL WINAPI
778 DllMain(HINSTANCE hInstDLL,
779 DWORD Reason,
780 LPVOID lpv)
782 TRACE("(%p,%x,%p)\n", hInstDLL, Reason, lpv);
783 if (Reason == DLL_PROCESS_ATTACH)
785 char buffer[MAX_PATH+10];
786 DWORD size = sizeof(buffer);
787 HKEY hkey = 0;
788 HKEY appkey = 0;
789 WNDCLASSA wc;
790 DWORD len;
792 /* Register the window class. This is used to create a hidden window
793 * for D3D rendering, if the application didn't pass one. It can also
794 * be used for creating a device window from SetCooperativeLevel(). */
795 wc.style = CS_HREDRAW | CS_VREDRAW;
796 wc.lpfnWndProc = DefWindowProcA;
797 wc.cbClsExtra = 0;
798 wc.cbWndExtra = 0;
799 wc.hInstance = hInstDLL;
800 wc.hIcon = 0;
801 wc.hCursor = 0;
802 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
803 wc.lpszMenuName = NULL;
804 wc.lpszClassName = DDRAW_WINDOW_CLASS_NAME;
805 if (!RegisterClassA(&wc))
807 ERR("Failed to register ddraw window class, last error %#x.\n", GetLastError());
808 return FALSE;
811 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
812 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
814 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
815 if (len && len < MAX_PATH)
817 HKEY tmpkey;
818 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
819 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
821 char *p, *appname = buffer;
822 if ((p = strrchr( appname, '/' ))) appname = p + 1;
823 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
824 strcat( appname, "\\Direct3D" );
825 TRACE("appname = [%s]\n", appname);
826 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
827 RegCloseKey( tmpkey );
831 if ( 0 != hkey || 0 != appkey )
833 if ( !get_config_key( hkey, appkey, "DirectDrawRenderer", buffer, size) )
835 if (!strcmp(buffer,"gdi"))
837 TRACE("Defaulting to GDI surfaces\n");
838 DefaultSurfaceType = SURFACE_GDI;
840 else if (!strcmp(buffer,"opengl"))
842 TRACE("Defaulting to opengl surfaces\n");
843 DefaultSurfaceType = SURFACE_OPENGL;
845 else
847 ERR("Unknown default surface type. Supported are:\n gdi, opengl\n");
852 /* On Windows one can force the refresh rate that DirectDraw uses by
853 * setting an override value in dxdiag. This is documented in KB315614
854 * (main article), KB230002, and KB217348. By comparing registry dumps
855 * before and after setting the override, we see that the override value
856 * is stored in HKLM\Software\Microsoft\DirectDraw\ForceRefreshRate as a
857 * DWORD that represents the refresh rate to force. We use this
858 * registry entry to modify the behavior of SetDisplayMode so that Wine
859 * users can override the refresh rate in a Windows-compatible way.
861 * dxdiag will not accept a refresh rate lower than 40 or higher than
862 * 120 so this value should be within that range. It is, of course,
863 * possible for a user to set the registry entry value directly so that
864 * assumption might not hold.
866 * There is no current mechanism for setting this value through the Wine
867 * GUI. It would be most appropriate to set this value through a dxdiag
868 * clone, but it may be sufficient to use winecfg.
870 * TODO: Create a mechanism for setting this value through the Wine GUI.
872 if ( !RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectDraw", &hkey ) )
874 DWORD type, data;
875 size = sizeof(data);
876 if (!RegQueryValueExA( hkey, "ForceRefreshRate", NULL, &type, (LPBYTE)&data, &size ) && type == REG_DWORD)
878 TRACE("ForceRefreshRate set; overriding refresh rate to %d Hz\n", data);
879 force_refresh_rate = data;
881 RegCloseKey( hkey );
884 DisableThreadLibraryCalls(hInstDLL);
886 else if (Reason == DLL_PROCESS_DETACH)
888 if(!list_empty(&global_ddraw_list))
890 struct list *entry, *entry2;
891 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
893 /* We remove elements from this loop */
894 LIST_FOR_EACH_SAFE(entry, entry2, &global_ddraw_list)
896 HRESULT hr;
897 DDSURFACEDESC2 desc;
898 int i;
899 IDirectDrawImpl *ddraw = LIST_ENTRY(entry, IDirectDrawImpl, ddraw_list_entry);
901 WARN("DDraw %p has a refcount of %d\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref3 + ddraw->ref2 + ddraw->ref1);
903 /* Add references to each interface to avoid freeing them unexpectedly */
904 IDirectDraw_AddRef((IDirectDraw *)&ddraw->IDirectDraw_vtbl);
905 IDirectDraw2_AddRef((IDirectDraw2 *)&ddraw->IDirectDraw2_vtbl);
906 IDirectDraw3_AddRef((IDirectDraw3 *)&ddraw->IDirectDraw3_vtbl);
907 IDirectDraw4_AddRef((IDirectDraw4 *)&ddraw->IDirectDraw4_vtbl);
908 IDirectDraw7_AddRef((IDirectDraw7 *)ddraw);
910 /* Does a D3D device exist? Destroy it
911 * TODO: Destroy all Vertex buffers, Lights, Materials
912 * and execute buffers too
914 if(ddraw->d3ddevice)
916 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
917 while(IDirect3DDevice7_Release((IDirect3DDevice7 *)ddraw->d3ddevice));
920 /* Try to release the objects
921 * Do an EnumSurfaces to find any hanging surfaces
923 memset(&desc, 0, sizeof(desc));
924 desc.dwSize = sizeof(desc);
925 for(i = 0; i <= 1; i++)
927 hr = IDirectDraw7_EnumSurfaces((IDirectDraw7 *)ddraw,
928 DDENUMSURFACES_ALL, &desc, ddraw, DestroyCallback);
929 if(hr != D3D_OK)
930 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
933 /* Check the surface count */
934 if(ddraw->surfaces > 0)
935 ERR("DDraw %p still has %d surfaces attached\n", ddraw, ddraw->surfaces);
937 /* Release all hanging references to destroy the objects. This
938 * restores the screen mode too
940 while(IDirectDraw_Release((IDirectDraw *)&ddraw->IDirectDraw_vtbl));
941 while(IDirectDraw2_Release((IDirectDraw2 *)&ddraw->IDirectDraw2_vtbl));
942 while(IDirectDraw3_Release((IDirectDraw3 *)&ddraw->IDirectDraw3_vtbl));
943 while(IDirectDraw4_Release((IDirectDraw4 *)&ddraw->IDirectDraw4_vtbl));
944 while(IDirectDraw7_Release((IDirectDraw7 *)ddraw));
948 /* Unregister the window class. */
949 UnregisterClassA(DDRAW_WINDOW_CLASS_NAME, hInstDLL);
952 return TRUE;