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
28 #include "wine/port.h"
30 #define DDRAW_INIT_GUID
31 #include "ddraw_private.h"
34 #include "wine/exception.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
39 /* The configured default surface */
40 enum ddraw_surface_type DefaultSurfaceType
= DDRAW_SURFACE_TYPE_OPENGL
;
42 static struct list global_ddraw_list
= LIST_INIT(global_ddraw_list
);
44 static HINSTANCE instance
;
46 /* value of ForceRefreshRate */
47 DWORD force_refresh_rate
= 0;
49 /* Structure for converting DirectDrawEnumerateA to DirectDrawEnumerateExA */
52 LPDDENUMCALLBACKA callback
;
56 /* Enumeration callback for converting DirectDrawEnumerateA to DirectDrawEnumerateExA */
57 static HRESULT CALLBACK
enum_callback(GUID
*guid
, char *description
, char *driver_name
,
58 void *context
, HMONITOR monitor
)
60 const struct callback_info
*info
= context
;
62 return info
->callback(guid
, description
, driver_name
, info
->context
);
65 /* Handle table functions */
66 BOOL
ddraw_handle_table_init(struct ddraw_handle_table
*t
, UINT initial_size
)
68 t
->entries
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, initial_size
* sizeof(*t
->entries
));
71 ERR("Failed to allocate handle table memory.\n");
74 t
->free_entries
= NULL
;
75 t
->table_size
= initial_size
;
81 void ddraw_handle_table_destroy(struct ddraw_handle_table
*t
)
83 HeapFree(GetProcessHeap(), 0, t
->entries
);
84 memset(t
, 0, sizeof(*t
));
87 DWORD
ddraw_allocate_handle(struct ddraw_handle_table
*t
, void *object
, enum ddraw_handle_type type
)
89 struct ddraw_handle_entry
*entry
;
93 DWORD idx
= t
->free_entries
- t
->entries
;
94 /* Use a free handle */
95 entry
= t
->free_entries
;
96 if (entry
->type
!= DDRAW_HANDLE_FREE
)
98 ERR("Handle %#x (%p) is in the free list, but has type %#x.\n", idx
, entry
->object
, entry
->type
);
99 return DDRAW_INVALID_HANDLE
;
101 t
->free_entries
= entry
->object
;
102 entry
->object
= object
;
108 if (!(t
->entry_count
< t
->table_size
))
111 UINT new_size
= t
->table_size
+ (t
->table_size
>> 1);
112 struct ddraw_handle_entry
*new_entries
= HeapReAlloc(GetProcessHeap(),
113 0, t
->entries
, new_size
* sizeof(*t
->entries
));
116 ERR("Failed to grow the handle table.\n");
117 return DDRAW_INVALID_HANDLE
;
119 t
->entries
= new_entries
;
120 t
->table_size
= new_size
;
123 entry
= &t
->entries
[t
->entry_count
];
124 entry
->object
= object
;
127 return t
->entry_count
++;
130 void *ddraw_free_handle(struct ddraw_handle_table
*t
, DWORD handle
, enum ddraw_handle_type type
)
132 struct ddraw_handle_entry
*entry
;
135 if (handle
== DDRAW_INVALID_HANDLE
|| handle
>= t
->entry_count
)
137 WARN("Invalid handle %#x passed.\n", handle
);
141 entry
= &t
->entries
[handle
];
142 if (entry
->type
!= type
)
144 WARN("Handle %#x (%p) is not of type %#x.\n", handle
, entry
->object
, type
);
148 object
= entry
->object
;
149 entry
->object
= t
->free_entries
;
150 entry
->type
= DDRAW_HANDLE_FREE
;
151 t
->free_entries
= entry
;
156 void *ddraw_get_object(struct ddraw_handle_table
*t
, DWORD handle
, enum ddraw_handle_type type
)
158 struct ddraw_handle_entry
*entry
;
160 if (handle
== DDRAW_INVALID_HANDLE
|| handle
>= t
->entry_count
)
162 WARN("Invalid handle %#x passed.\n", handle
);
166 entry
= &t
->entries
[handle
];
167 if (entry
->type
!= type
)
169 WARN("Handle %#x (%p) is not of type %#x.\n", handle
, entry
->object
, type
);
173 return entry
->object
;
176 /***********************************************************************
178 * Helper function for DirectDrawCreate and friends
179 * Creates a new DDraw interface with the given REFIID
181 * Interfaces that can be created:
182 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
183 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
184 * IDirect3D interfaces?)
187 * guid: ID of the requested driver, NULL for the default driver.
188 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
189 * DD: Used to return the pointer to the created object
190 * UnkOuter: For aggregation, which is unsupported. Must be NULL
191 * iid: requested version ID.
194 * DD_OK if the Interface was created successfully
195 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
196 * E_OUTOFMEMORY if some allocation failed
198 ***********************************************************************/
200 DDRAW_Create(const GUID
*guid
,
205 enum wined3d_device_type device_type
;
209 TRACE("driver_guid %s, ddraw %p, outer_unknown %p, interface_iid %s.\n",
210 debugstr_guid(guid
), DD
, UnkOuter
, debugstr_guid(iid
));
214 /* We don't care about this guids. Well, there's no special guid anyway
217 if (guid
== (GUID
*) DDCREATE_EMULATIONONLY
)
219 /* Use the reference device id. This doesn't actually change anything,
220 * WineD3D always uses OpenGL for D3D rendering. One could make it request
223 device_type
= WINED3D_DEVICE_TYPE_REF
;
225 else if(guid
== (GUID
*) DDCREATE_HARDWAREONLY
)
227 device_type
= WINED3D_DEVICE_TYPE_HAL
;
234 /* DDraw doesn't support aggregation, according to msdn */
235 if (UnkOuter
!= NULL
)
236 return CLASS_E_NOAGGREGATION
;
238 /* DirectDraw creation comes here */
239 ddraw
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ddraw
));
242 ERR("Out of memory when creating DirectDraw\n");
243 return E_OUTOFMEMORY
;
246 hr
= ddraw_init(ddraw
, device_type
);
249 WARN("Failed to initialize ddraw object, hr %#x.\n", hr
);
250 HeapFree(GetProcessHeap(), 0, ddraw
);
254 hr
= IDirectDraw7_QueryInterface(&ddraw
->IDirectDraw7_iface
, iid
, DD
);
255 IDirectDraw7_Release(&ddraw
->IDirectDraw7_iface
);
257 list_add_head(&global_ddraw_list
, &ddraw
->ddraw_list_entry
);
259 WARN("Failed to query interface %s from ddraw object %p.\n", debugstr_guid(iid
), ddraw
);
264 /***********************************************************************
265 * DirectDrawCreate (DDRAW.@)
267 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
268 * interfaces in theory
270 * Arguments, return values: See DDRAW_Create
272 ***********************************************************************/
273 HRESULT WINAPI DECLSPEC_HOTPATCH
DirectDrawCreate(GUID
*driver_guid
, IDirectDraw
**ddraw
, IUnknown
*outer
)
277 TRACE("driver_guid %s, ddraw %p, outer %p.\n",
278 debugstr_guid(driver_guid
), ddraw
, outer
);
280 wined3d_mutex_lock();
281 hr
= DDRAW_Create(driver_guid
, (void **)ddraw
, outer
, &IID_IDirectDraw
);
282 wined3d_mutex_unlock();
286 if (FAILED(hr
= IDirectDraw_Initialize(*ddraw
, driver_guid
)))
287 IDirectDraw_Release(*ddraw
);
293 /***********************************************************************
294 * DirectDrawCreateEx (DDRAW.@)
296 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
297 * interfaces are requested.
299 * Arguments, return values: See DDRAW_Create
301 ***********************************************************************/
302 HRESULT WINAPI DECLSPEC_HOTPATCH
303 DirectDrawCreateEx(GUID
*guid
,
310 TRACE("driver_guid %s, ddraw %p, interface_iid %s, outer_unknown %p.\n",
311 debugstr_guid(guid
), dd
, debugstr_guid(iid
), UnkOuter
);
313 if (!IsEqualGUID(iid
, &IID_IDirectDraw7
))
314 return DDERR_INVALIDPARAMS
;
316 wined3d_mutex_lock();
317 hr
= DDRAW_Create(guid
, dd
, UnkOuter
, iid
);
318 wined3d_mutex_unlock();
322 IDirectDraw7
*ddraw7
= *(IDirectDraw7
**)dd
;
323 hr
= IDirectDraw7_Initialize(ddraw7
, guid
);
325 IDirectDraw7_Release(ddraw7
);
331 /***********************************************************************
332 * DirectDrawEnumerateA (DDRAW.@)
334 * Enumerates legacy ddraw drivers, ascii version. We only have one
335 * driver, which relays to WineD3D. If we were sufficiently cool,
336 * we could offer various interfaces, which use a different default surface
337 * implementation, but I think it's better to offer this choice in
338 * winecfg, because some apps use the default driver, so we would need
339 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
342 * Callback: Callback function from the app
343 * Context: Argument to the call back.
347 * E_INVALIDARG if the Callback caused a page fault
350 ***********************************************************************/
351 HRESULT WINAPI
DirectDrawEnumerateA(LPDDENUMCALLBACKA callback
, void *context
)
353 struct callback_info info
;
355 TRACE("callback %p, context %p.\n", callback
, context
);
357 info
.callback
= callback
;
358 info
.context
= context
;
359 return DirectDrawEnumerateExA(enum_callback
, &info
, 0x0);
362 /***********************************************************************
363 * DirectDrawEnumerateExA (DDRAW.@)
365 * Enumerates DirectDraw7 drivers, ascii version. See
366 * the comments above DirectDrawEnumerateA for more details.
368 * The Flag member is not supported right now.
370 ***********************************************************************/
371 HRESULT WINAPI
DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA callback
, void *context
, DWORD flags
)
373 struct wined3d
*wined3d
;
376 TRACE("callback %p, context %p, flags %#x.\n", callback
, context
, flags
);
378 if (flags
& ~(DDENUM_ATTACHEDSECONDARYDEVICES
|
379 DDENUM_DETACHEDSECONDARYDEVICES
|
380 DDENUM_NONDISPLAYDEVICES
))
381 return DDERR_INVALIDPARAMS
;
384 FIXME("flags 0x%08x not handled\n", flags
);
386 wined3d_flags
= WINED3D_LEGACY_DEPTH_BIAS
;
387 if (DefaultSurfaceType
!= DDRAW_SURFACE_TYPE_OPENGL
)
388 wined3d_flags
|= WINED3D_NO3D
;
390 TRACE("Enumerating ddraw interfaces\n");
391 if (!(wined3d
= wined3d_create(7, wined3d_flags
)))
393 if ((wined3d_flags
& WINED3D_NO3D
) || !(wined3d
= wined3d_create(7, wined3d_flags
| WINED3D_NO3D
)))
395 WARN("Failed to create a wined3d object.\n");
399 WARN("Created a wined3d object without 3D support.\n");
400 DefaultSurfaceType
= DDRAW_SURFACE_TYPE_GDI
;
405 /* QuickTime expects the description "DirectDraw HAL" */
406 static CHAR driver_desc
[] = "DirectDraw HAL",
407 driver_name
[] = "display";
408 struct wined3d_adapter_identifier adapter_id
;
413 /* The Battle.net System Checker expects both a NULL device and a GUID-based device */
414 TRACE("Default interface: DirectDraw HAL\n");
415 cont_enum
= callback(NULL
, driver_desc
, driver_name
, context
, 0);
416 for (adapter
= 0; SUCCEEDED(hr
) && cont_enum
; adapter
++)
418 char DriverName
[512] = "";
420 /* The Battle.net System Checker expects the GetAdapterIdentifier DeviceName to match the
421 * Driver Name, so obtain the DeviceName and GUID from D3D. */
422 memset(&adapter_id
, 0x0, sizeof(adapter_id
));
423 adapter_id
.device_name
= DriverName
;
424 adapter_id
.device_name_size
= sizeof(DriverName
);
425 wined3d_mutex_lock();
426 hr
= wined3d_get_adapter_identifier(wined3d
, adapter
, 0x0, &adapter_id
);
427 wined3d_mutex_unlock();
430 TRACE("Interface %d: %s\n", adapter
, wine_dbgstr_guid(&adapter_id
.device_identifier
));
431 cont_enum
= callback(&adapter_id
.device_identifier
, driver_desc
,
432 adapter_id
.device_name
, context
, 0);
438 wined3d_decref(wined3d
);
439 return DDERR_INVALIDPARAMS
;
443 wined3d_decref(wined3d
);
444 TRACE("End of enumeration\n");
448 /***********************************************************************
449 * DirectDrawEnumerateW (DDRAW.@)
451 * Enumerates legacy drivers, unicode version.
452 * This function is not implemented on Windows.
454 ***********************************************************************/
455 HRESULT WINAPI
DirectDrawEnumerateW(LPDDENUMCALLBACKW callback
, void *context
)
457 TRACE("callback %p, context %p.\n", callback
, context
);
460 return DDERR_INVALIDPARAMS
;
462 return DDERR_UNSUPPORTED
;
465 /***********************************************************************
466 * DirectDrawEnumerateExW (DDRAW.@)
468 * Enumerates DirectDraw7 drivers, unicode version.
469 * This function is not implemented on Windows.
471 ***********************************************************************/
472 HRESULT WINAPI
DirectDrawEnumerateExW(LPDDENUMCALLBACKEXW callback
, void *context
, DWORD flags
)
474 TRACE("callback %p, context %p, flags %#x.\n", callback
, context
, flags
);
476 return DDERR_UNSUPPORTED
;
479 /***********************************************************************
480 * Classfactory implementation.
481 ***********************************************************************/
483 /***********************************************************************
484 * CF_CreateDirectDraw
486 * DDraw creation function for the class factory
489 * UnkOuter: Set to NULL
490 * iid: ID of the wanted interface
491 * obj: Address to pass the interface pointer back
494 * DD_OK / DDERR*, see DDRAW_Create
496 ***********************************************************************/
498 CF_CreateDirectDraw(IUnknown
* UnkOuter
, REFIID iid
,
503 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter
, debugstr_guid(iid
), obj
);
505 wined3d_mutex_lock();
506 hr
= DDRAW_Create(NULL
, obj
, UnkOuter
, iid
);
507 wined3d_mutex_unlock();
512 /***********************************************************************
513 * CF_CreateDirectDraw
515 * Clipper creation function for the class factory
518 * UnkOuter: Set to NULL
519 * iid: ID of the wanted interface
520 * obj: Address to pass the interface pointer back
523 * DD_OK / DDERR*, see DDRAW_Create
525 ***********************************************************************/
527 CF_CreateDirectDrawClipper(IUnknown
* UnkOuter
, REFIID riid
,
531 IDirectDrawClipper
*Clip
;
533 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter
, debugstr_guid(riid
), obj
);
535 wined3d_mutex_lock();
536 hr
= DirectDrawCreateClipper(0, &Clip
, UnkOuter
);
539 wined3d_mutex_unlock();
543 hr
= IDirectDrawClipper_QueryInterface(Clip
, riid
, obj
);
544 IDirectDrawClipper_Release(Clip
);
546 wined3d_mutex_unlock();
551 static const struct object_creation_info object_creation
[] =
553 { &CLSID_DirectDraw
, CF_CreateDirectDraw
},
554 { &CLSID_DirectDraw7
, CF_CreateDirectDraw
},
555 { &CLSID_DirectDrawClipper
, CF_CreateDirectDrawClipper
}
558 struct ddraw_class_factory
560 IClassFactory IClassFactory_iface
;
563 HRESULT (*pfnCreateInstance
)(IUnknown
*pUnkOuter
, REFIID iid
, LPVOID
*ppObj
);
566 static inline struct ddraw_class_factory
*impl_from_IClassFactory(IClassFactory
*iface
)
568 return CONTAINING_RECORD(iface
, struct ddraw_class_factory
, IClassFactory_iface
);
571 /*******************************************************************************
572 * IDirectDrawClassFactory::QueryInterface
574 * QueryInterface for the class factory
577 * riid Reference to identifier of queried interface
578 * ppv Address to return the interface pointer at
582 * Failure: E_NOINTERFACE
584 *******************************************************************************/
585 static HRESULT WINAPI
ddraw_class_factory_QueryInterface(IClassFactory
*iface
, REFIID riid
, void **out
)
587 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
589 if (IsEqualGUID(riid
, &IID_IUnknown
)
590 || IsEqualGUID(riid
, &IID_IClassFactory
))
592 IClassFactory_AddRef(iface
);
597 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
599 return E_NOINTERFACE
;
602 /*******************************************************************************
603 * IDirectDrawClassFactory::AddRef
605 * AddRef for the class factory
610 *******************************************************************************/
611 static ULONG WINAPI
ddraw_class_factory_AddRef(IClassFactory
*iface
)
613 struct ddraw_class_factory
*factory
= impl_from_IClassFactory(iface
);
614 ULONG ref
= InterlockedIncrement(&factory
->ref
);
616 TRACE("%p increasing refcount to %u.\n", factory
, ref
);
621 /*******************************************************************************
622 * IDirectDrawClassFactory::Release
624 * Release for the class factory. If the refcount falls to 0, the object
630 *******************************************************************************/
631 static ULONG WINAPI
ddraw_class_factory_Release(IClassFactory
*iface
)
633 struct ddraw_class_factory
*factory
= impl_from_IClassFactory(iface
);
634 ULONG ref
= InterlockedDecrement(&factory
->ref
);
636 TRACE("%p decreasing refcount to %u.\n", factory
, ref
);
639 HeapFree(GetProcessHeap(), 0, factory
);
645 /*******************************************************************************
646 * IDirectDrawClassFactory::CreateInstance
648 * What is this? Seems to create DirectDraw objects...
651 * The usual things???
656 *******************************************************************************/
657 static HRESULT WINAPI
ddraw_class_factory_CreateInstance(IClassFactory
*iface
,
658 IUnknown
*outer_unknown
, REFIID riid
, void **out
)
660 struct ddraw_class_factory
*factory
= impl_from_IClassFactory(iface
);
662 TRACE("iface %p, outer_unknown %p, riid %s, out %p.\n",
663 iface
, outer_unknown
, debugstr_guid(riid
), out
);
665 return factory
->pfnCreateInstance(outer_unknown
, riid
, out
);
668 /*******************************************************************************
669 * IDirectDrawClassFactory::LockServer
677 * S_OK, because it's a stub
679 *******************************************************************************/
680 static HRESULT WINAPI
ddraw_class_factory_LockServer(IClassFactory
*iface
, BOOL dolock
)
682 FIXME("iface %p, dolock %#x stub!\n", iface
, dolock
);
687 /*******************************************************************************
688 * The class factory VTable
689 *******************************************************************************/
690 static const IClassFactoryVtbl IClassFactory_Vtbl
=
692 ddraw_class_factory_QueryInterface
,
693 ddraw_class_factory_AddRef
,
694 ddraw_class_factory_Release
,
695 ddraw_class_factory_CreateInstance
,
696 ddraw_class_factory_LockServer
699 /*******************************************************************************
700 * DllGetClassObject [DDRAW.@]
701 * Retrieves class object from a DLL object
704 * Docs say returns STDAPI
707 * rclsid [I] CLSID for the class object
708 * riid [I] Reference to identifier of interface for class object
709 * ppv [O] Address of variable to receive interface pointer for riid
713 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
716 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
718 struct ddraw_class_factory
*factory
;
721 TRACE("rclsid %s, riid %s, object %p.\n",
722 debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
724 if (!IsEqualGUID(&IID_IClassFactory
, riid
)
725 && !IsEqualGUID(&IID_IUnknown
, riid
))
726 return E_NOINTERFACE
;
728 for (i
=0; i
< sizeof(object_creation
)/sizeof(object_creation
[0]); i
++)
730 if (IsEqualGUID(object_creation
[i
].clsid
, rclsid
))
734 if (i
== sizeof(object_creation
)/sizeof(object_creation
[0]))
736 FIXME("%s: no class found.\n", debugstr_guid(rclsid
));
737 return CLASS_E_CLASSNOTAVAILABLE
;
740 factory
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*factory
));
741 if (factory
== NULL
) return E_OUTOFMEMORY
;
743 factory
->IClassFactory_iface
.lpVtbl
= &IClassFactory_Vtbl
;
746 factory
->pfnCreateInstance
= object_creation
[i
].pfnCreateInstance
;
753 /*******************************************************************************
754 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
760 HRESULT WINAPI
DllCanUnloadNow(void)
768 /***********************************************************************
769 * DllRegisterServer (DDRAW.@)
771 HRESULT WINAPI
DllRegisterServer(void)
773 return __wine_register_resources( instance
);
776 /***********************************************************************
777 * DllUnregisterServer (DDRAW.@)
779 HRESULT WINAPI
DllUnregisterServer(void)
781 return __wine_unregister_resources( instance
);
784 /*******************************************************************************
787 * Callback function for the EnumSurfaces call in DllMain.
788 * Dumps some surface info and releases the surface
791 * surf: The enumerated surface
792 * desc: it's description
793 * context: Pointer to the ddraw impl
797 *******************************************************************************/
798 static HRESULT WINAPI
799 DestroyCallback(IDirectDrawSurface7
*surf
,
800 DDSURFACEDESC2
*desc
,
803 struct ddraw_surface
*Impl
= impl_from_IDirectDrawSurface7(surf
);
804 ULONG ref7
, ref4
, ref3
, ref2
, ref1
, gamma_count
, iface_count
;
806 ref7
= IDirectDrawSurface7_Release(surf
); /* For the EnumSurfaces */
811 gamma_count
= Impl
->gamma_count
;
813 WARN("Surface %p has an reference counts of 7: %u 4: %u 3: %u 2: %u 1: %u gamma: %u\n",
814 Impl
, ref7
, ref4
, ref3
, ref2
, ref1
, gamma_count
);
816 /* Skip surfaces which are attached somewhere or which are
817 * part of a complex compound. They will get released when destroying
820 if( (!Impl
->is_complex_root
) || (Impl
->first_attached
!= Impl
) )
823 /* Destroy the surface */
824 iface_count
= ddraw_surface_release_iface(Impl
);
825 while (iface_count
) iface_count
= ddraw_surface_release_iface(Impl
);
830 /***********************************************************************
833 * Reads a config key from the registry. Taken from WineD3D
835 ***********************************************************************/
836 static inline DWORD
get_config_key(HKEY defkey
, HKEY appkey
, const char* name
, char* buffer
, DWORD size
)
838 if (0 != appkey
&& !RegQueryValueExA( appkey
, name
, 0, NULL
, (LPBYTE
) buffer
, &size
)) return 0;
839 if (0 != defkey
&& !RegQueryValueExA( defkey
, name
, 0, NULL
, (LPBYTE
) buffer
, &size
)) return 0;
840 return ERROR_FILE_NOT_FOUND
;
843 /***********************************************************************
846 * Could be used to register DirectDraw drivers, if we have more than
847 * one. Also used to destroy any objects left at unload if the
848 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
850 ***********************************************************************/
852 DllMain(HINSTANCE hInstDLL
,
856 TRACE("(%p,%x,%p)\n", hInstDLL
, Reason
, lpv
);
857 if (Reason
== DLL_PROCESS_ATTACH
)
859 static HMODULE ddraw_self
;
860 char buffer
[MAX_PATH
+10];
861 DWORD size
= sizeof(buffer
);
867 /* Register the window class. This is used to create a hidden window
868 * for D3D rendering, if the application didn't pass one. It can also
869 * be used for creating a device window from SetCooperativeLevel(). */
870 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
871 wc
.lpfnWndProc
= DefWindowProcA
;
874 wc
.hInstance
= hInstDLL
;
877 wc
.hbrBackground
= GetStockObject(BLACK_BRUSH
);
878 wc
.lpszMenuName
= NULL
;
879 wc
.lpszClassName
= DDRAW_WINDOW_CLASS_NAME
;
880 if (!RegisterClassA(&wc
))
882 ERR("Failed to register ddraw window class, last error %#x.\n", GetLastError());
886 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
887 if ( RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Direct3D", &hkey
) ) hkey
= 0;
889 len
= GetModuleFileNameA( 0, buffer
, MAX_PATH
);
890 if (len
&& len
< MAX_PATH
)
893 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
894 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
896 char *p
, *appname
= buffer
;
897 if ((p
= strrchr( appname
, '/' ))) appname
= p
+ 1;
898 if ((p
= strrchr( appname
, '\\' ))) appname
= p
+ 1;
899 strcat( appname
, "\\Direct3D" );
900 TRACE("appname = [%s]\n", appname
);
901 if (RegOpenKeyA( tmpkey
, appname
, &appkey
)) appkey
= 0;
902 RegCloseKey( tmpkey
);
906 if ( 0 != hkey
|| 0 != appkey
)
908 if ( !get_config_key( hkey
, appkey
, "DirectDrawRenderer", buffer
, size
) )
910 if (!strcmp(buffer
,"gdi"))
912 TRACE("Defaulting to GDI surfaces\n");
913 DefaultSurfaceType
= DDRAW_SURFACE_TYPE_GDI
;
915 else if (!strcmp(buffer
,"opengl"))
917 TRACE("Defaulting to opengl surfaces\n");
918 DefaultSurfaceType
= DDRAW_SURFACE_TYPE_OPENGL
;
922 ERR("Unknown default surface type. Supported are:\n gdi, opengl\n");
927 /* On Windows one can force the refresh rate that DirectDraw uses by
928 * setting an override value in dxdiag. This is documented in KB315614
929 * (main article), KB230002, and KB217348. By comparing registry dumps
930 * before and after setting the override, we see that the override value
931 * is stored in HKLM\Software\Microsoft\DirectDraw\ForceRefreshRate as a
932 * DWORD that represents the refresh rate to force. We use this
933 * registry entry to modify the behavior of SetDisplayMode so that Wine
934 * users can override the refresh rate in a Windows-compatible way.
936 * dxdiag will not accept a refresh rate lower than 40 or higher than
937 * 120 so this value should be within that range. It is, of course,
938 * possible for a user to set the registry entry value directly so that
939 * assumption might not hold.
941 * There is no current mechanism for setting this value through the Wine
942 * GUI. It would be most appropriate to set this value through a dxdiag
943 * clone, but it may be sufficient to use winecfg.
945 * TODO: Create a mechanism for setting this value through the Wine GUI.
947 if ( !RegOpenKeyA( HKEY_LOCAL_MACHINE
, "Software\\Microsoft\\DirectDraw", &hkey
) )
951 if (!RegQueryValueExA( hkey
, "ForceRefreshRate", NULL
, &type
, (LPBYTE
)&data
, &size
) && type
== REG_DWORD
)
953 TRACE("ForceRefreshRate set; overriding refresh rate to %d Hz\n", data
);
954 force_refresh_rate
= data
;
959 /* Prevent the ddraw module from being unloaded. When switching to
960 * exclusive mode, we replace the window proc of the ddraw window. If
961 * an application would unload ddraw from the WM_DESTROY handler for
962 * that window, it would return to unmapped memory and die. Apparently
963 * this is supposed to work on Windows. We should probably use
964 * GET_MODULE_HANDLE_EX_FLAG_PIN for this, but that's not currently
966 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (const WCHAR
*)&ddraw_self
, &ddraw_self
))
967 ERR("Failed to get own module handle.\n");
970 DisableThreadLibraryCalls(hInstDLL
);
972 else if (Reason
== DLL_PROCESS_DETACH
)
974 if(!list_empty(&global_ddraw_list
))
976 struct list
*entry
, *entry2
;
977 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
979 /* We remove elements from this loop */
980 LIST_FOR_EACH_SAFE(entry
, entry2
, &global_ddraw_list
)
982 struct ddraw
*ddraw
= LIST_ENTRY(entry
, struct ddraw
, ddraw_list_entry
);
987 WARN("DDraw %p has a refcount of %d\n", ddraw
, ddraw
->ref7
+ ddraw
->ref4
+ ddraw
->ref3
+ ddraw
->ref2
+ ddraw
->ref1
);
989 /* Add references to each interface to avoid freeing them unexpectedly */
990 IDirectDraw_AddRef(&ddraw
->IDirectDraw_iface
);
991 IDirectDraw2_AddRef(&ddraw
->IDirectDraw2_iface
);
992 IDirectDraw4_AddRef(&ddraw
->IDirectDraw4_iface
);
993 IDirectDraw7_AddRef(&ddraw
->IDirectDraw7_iface
);
995 /* Does a D3D device exist? Destroy it
996 * TODO: Destroy all Vertex buffers, Lights, Materials
997 * and execute buffers too
1001 WARN("DDraw %p has d3ddevice %p attached\n", ddraw
, ddraw
->d3ddevice
);
1002 while(IDirect3DDevice7_Release(&ddraw
->d3ddevice
->IDirect3DDevice7_iface
));
1005 /* Destroy the swapchain after any 3D device. The 3D device
1006 * cleanup code needs a swapchain. Specifically, it tries to
1007 * set the current render target to the front buffer. */
1008 if (ddraw
->wined3d_swapchain
)
1009 ddraw_destroy_swapchain(ddraw
);
1011 /* Try to release the objects
1012 * Do an EnumSurfaces to find any hanging surfaces
1014 memset(&desc
, 0, sizeof(desc
));
1015 desc
.dwSize
= sizeof(desc
);
1016 for(i
= 0; i
<= 1; i
++)
1018 hr
= IDirectDraw7_EnumSurfaces(&ddraw
->IDirectDraw7_iface
, DDENUMSURFACES_ALL
,
1019 &desc
, ddraw
, DestroyCallback
);
1021 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw
);
1024 if (!list_empty(&ddraw
->surface_list
))
1025 ERR("DDraw %p still has surfaces attached.\n", ddraw
);
1027 /* Release all hanging references to destroy the objects. This
1028 * restores the screen mode too
1030 while(IDirectDraw_Release(&ddraw
->IDirectDraw_iface
));
1031 while(IDirectDraw2_Release(&ddraw
->IDirectDraw2_iface
));
1032 while(IDirectDraw4_Release(&ddraw
->IDirectDraw4_iface
));
1033 while(IDirectDraw7_Release(&ddraw
->IDirectDraw7_iface
));
1037 /* Unregister the window class. */
1038 UnregisterClassA(DDRAW_WINDOW_CLASS_NAME
, hInstDLL
);