ddraw: Handle the "DirectDrawRenderer" registry key in wined3d.
[wine/multimedia.git] / dlls / ddraw / main.c
blob8e7b09893bf108a760d205a07b7dec8899c3e281
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"
30 #define DDRAW_INIT_GUID
31 #include "ddraw_private.h"
32 #include "rpcproxy.h"
34 #include "wine/exception.h"
35 #include "winreg.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
39 static struct list global_ddraw_list = LIST_INIT(global_ddraw_list);
41 static HINSTANCE instance;
43 /* value of ForceRefreshRate */
44 DWORD force_refresh_rate = 0;
46 /* Structure for converting DirectDrawEnumerateA to DirectDrawEnumerateExA */
47 struct callback_info
49 LPDDENUMCALLBACKA callback;
50 void *context;
53 /* Enumeration callback for converting DirectDrawEnumerateA to DirectDrawEnumerateExA */
54 static HRESULT CALLBACK enum_callback(GUID *guid, char *description, char *driver_name,
55 void *context, HMONITOR monitor)
57 const struct callback_info *info = context;
59 return info->callback(guid, description, driver_name, info->context);
62 /* Handle table functions */
63 BOOL ddraw_handle_table_init(struct ddraw_handle_table *t, UINT initial_size)
65 t->entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, initial_size * sizeof(*t->entries));
66 if (!t->entries)
68 ERR("Failed to allocate handle table memory.\n");
69 return FALSE;
71 t->free_entries = NULL;
72 t->table_size = initial_size;
73 t->entry_count = 0;
75 return TRUE;
78 void ddraw_handle_table_destroy(struct ddraw_handle_table *t)
80 HeapFree(GetProcessHeap(), 0, t->entries);
81 memset(t, 0, sizeof(*t));
84 DWORD ddraw_allocate_handle(struct ddraw_handle_table *t, void *object, enum ddraw_handle_type type)
86 struct ddraw_handle_entry *entry;
88 if (t->free_entries)
90 DWORD idx = t->free_entries - t->entries;
91 /* Use a free handle */
92 entry = t->free_entries;
93 if (entry->type != DDRAW_HANDLE_FREE)
95 ERR("Handle %#x (%p) is in the free list, but has type %#x.\n", idx, entry->object, entry->type);
96 return DDRAW_INVALID_HANDLE;
98 t->free_entries = entry->object;
99 entry->object = object;
100 entry->type = type;
102 return idx;
105 if (!(t->entry_count < t->table_size))
107 /* Grow the table */
108 UINT new_size = t->table_size + (t->table_size >> 1);
109 struct ddraw_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
110 0, t->entries, new_size * sizeof(*t->entries));
111 if (!new_entries)
113 ERR("Failed to grow the handle table.\n");
114 return DDRAW_INVALID_HANDLE;
116 t->entries = new_entries;
117 t->table_size = new_size;
120 entry = &t->entries[t->entry_count];
121 entry->object = object;
122 entry->type = type;
124 return t->entry_count++;
127 void *ddraw_free_handle(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
129 struct ddraw_handle_entry *entry;
130 void *object;
132 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
134 WARN("Invalid handle %#x passed.\n", handle);
135 return NULL;
138 entry = &t->entries[handle];
139 if (entry->type != type)
141 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
142 return NULL;
145 object = entry->object;
146 entry->object = t->free_entries;
147 entry->type = DDRAW_HANDLE_FREE;
148 t->free_entries = entry;
150 return object;
153 void *ddraw_get_object(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
155 struct ddraw_handle_entry *entry;
157 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
159 WARN("Invalid handle %#x passed.\n", handle);
160 return NULL;
163 entry = &t->entries[handle];
164 if (entry->type != type)
166 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
167 return NULL;
170 return entry->object;
173 /***********************************************************************
175 * Helper function for DirectDrawCreate and friends
176 * Creates a new DDraw interface with the given REFIID
178 * Interfaces that can be created:
179 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
180 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
181 * IDirect3D interfaces?)
183 * Arguments:
184 * guid: ID of the requested driver, NULL for the default driver.
185 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
186 * DD: Used to return the pointer to the created object
187 * UnkOuter: For aggregation, which is unsupported. Must be NULL
188 * iid: requested version ID.
190 * Returns:
191 * DD_OK if the Interface was created successfully
192 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
193 * E_OUTOFMEMORY if some allocation failed
195 ***********************************************************************/
196 static HRESULT
197 DDRAW_Create(const GUID *guid,
198 void **DD,
199 IUnknown *UnkOuter,
200 REFIID iid)
202 enum wined3d_device_type device_type;
203 struct ddraw *ddraw;
204 HRESULT hr;
206 TRACE("driver_guid %s, ddraw %p, outer_unknown %p, interface_iid %s.\n",
207 debugstr_guid(guid), DD, UnkOuter, debugstr_guid(iid));
209 *DD = NULL;
211 /* We don't care about this guids. Well, there's no special guid anyway
212 * OK, we could
214 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
216 /* Use the reference device id. This doesn't actually change anything,
217 * WineD3D always uses OpenGL for D3D rendering. One could make it request
218 * indirect rendering
220 device_type = WINED3D_DEVICE_TYPE_REF;
222 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
224 device_type = WINED3D_DEVICE_TYPE_HAL;
226 else
228 device_type = 0;
231 /* DDraw doesn't support aggregation, according to msdn */
232 if (UnkOuter != NULL)
233 return CLASS_E_NOAGGREGATION;
235 /* DirectDraw creation comes here */
236 ddraw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw));
237 if (!ddraw)
239 ERR("Out of memory when creating DirectDraw\n");
240 return E_OUTOFMEMORY;
243 hr = ddraw_init(ddraw, device_type);
244 if (FAILED(hr))
246 WARN("Failed to initialize ddraw object, hr %#x.\n", hr);
247 HeapFree(GetProcessHeap(), 0, ddraw);
248 return hr;
251 hr = IDirectDraw7_QueryInterface(&ddraw->IDirectDraw7_iface, iid, DD);
252 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
253 if (SUCCEEDED(hr))
254 list_add_head(&global_ddraw_list, &ddraw->ddraw_list_entry);
255 else
256 WARN("Failed to query interface %s from ddraw object %p.\n", debugstr_guid(iid), ddraw);
258 return hr;
261 /***********************************************************************
262 * DirectDrawCreate (DDRAW.@)
264 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
265 * interfaces in theory
267 * Arguments, return values: See DDRAW_Create
269 ***********************************************************************/
270 HRESULT WINAPI DECLSPEC_HOTPATCH DirectDrawCreate(GUID *driver_guid, IDirectDraw **ddraw, IUnknown *outer)
272 HRESULT hr;
274 TRACE("driver_guid %s, ddraw %p, outer %p.\n",
275 debugstr_guid(driver_guid), ddraw, outer);
277 wined3d_mutex_lock();
278 hr = DDRAW_Create(driver_guid, (void **)ddraw, outer, &IID_IDirectDraw);
279 wined3d_mutex_unlock();
281 if (SUCCEEDED(hr))
283 if (FAILED(hr = IDirectDraw_Initialize(*ddraw, driver_guid)))
284 IDirectDraw_Release(*ddraw);
287 return hr;
290 /***********************************************************************
291 * DirectDrawCreateEx (DDRAW.@)
293 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
294 * interfaces are requested.
296 * Arguments, return values: See DDRAW_Create
298 ***********************************************************************/
299 HRESULT WINAPI DECLSPEC_HOTPATCH
300 DirectDrawCreateEx(GUID *guid,
301 LPVOID *dd,
302 REFIID iid,
303 IUnknown *UnkOuter)
305 HRESULT hr;
307 TRACE("driver_guid %s, ddraw %p, interface_iid %s, outer_unknown %p.\n",
308 debugstr_guid(guid), dd, debugstr_guid(iid), UnkOuter);
310 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
311 return DDERR_INVALIDPARAMS;
313 wined3d_mutex_lock();
314 hr = DDRAW_Create(guid, dd, UnkOuter, iid);
315 wined3d_mutex_unlock();
317 if (SUCCEEDED(hr))
319 IDirectDraw7 *ddraw7 = *(IDirectDraw7 **)dd;
320 hr = IDirectDraw7_Initialize(ddraw7, guid);
321 if (FAILED(hr))
322 IDirectDraw7_Release(ddraw7);
325 return hr;
328 /***********************************************************************
329 * DirectDrawEnumerateA (DDRAW.@)
331 * Enumerates legacy ddraw drivers, ascii version. We only have one
332 * driver, which relays to WineD3D. If we were sufficiently cool,
333 * we could offer various interfaces, which use a different default surface
334 * implementation, but I think it's better to offer this choice in
335 * winecfg, because some apps use the default driver, so we would need
336 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
338 * Arguments:
339 * Callback: Callback function from the app
340 * Context: Argument to the call back.
342 * Returns:
343 * DD_OK on success
344 * E_INVALIDARG if the Callback caused a page fault
347 ***********************************************************************/
348 HRESULT WINAPI DirectDrawEnumerateA(LPDDENUMCALLBACKA callback, void *context)
350 struct callback_info info;
352 TRACE("callback %p, context %p.\n", callback, context);
354 info.callback = callback;
355 info.context = context;
356 return DirectDrawEnumerateExA(enum_callback, &info, 0x0);
359 /***********************************************************************
360 * DirectDrawEnumerateExA (DDRAW.@)
362 * Enumerates DirectDraw7 drivers, ascii version. See
363 * the comments above DirectDrawEnumerateA for more details.
365 * The Flag member is not supported right now.
367 ***********************************************************************/
368 HRESULT WINAPI DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA callback, void *context, DWORD flags)
370 struct wined3d *wined3d;
372 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
374 if (flags & ~(DDENUM_ATTACHEDSECONDARYDEVICES |
375 DDENUM_DETACHEDSECONDARYDEVICES |
376 DDENUM_NONDISPLAYDEVICES))
377 return DDERR_INVALIDPARAMS;
379 if (flags)
380 FIXME("flags 0x%08x not handled\n", flags);
382 TRACE("Enumerating ddraw interfaces\n");
383 if (!(wined3d = wined3d_create(7, WINED3D_LEGACY_DEPTH_BIAS)))
385 if (!(wined3d = wined3d_create(7, WINED3D_LEGACY_DEPTH_BIAS | WINED3D_NO3D)))
387 WARN("Failed to create a wined3d object.\n");
388 return E_FAIL;
391 WARN("Created a wined3d object without 3D support.\n");
394 __TRY
396 /* QuickTime expects the description "DirectDraw HAL" */
397 static CHAR driver_desc[] = "DirectDraw HAL",
398 driver_name[] = "display";
399 struct wined3d_adapter_identifier adapter_id;
400 HRESULT hr = S_OK;
401 UINT adapter = 0;
402 BOOL cont_enum;
404 /* The Battle.net System Checker expects both a NULL device and a GUID-based device */
405 TRACE("Default interface: DirectDraw HAL\n");
406 cont_enum = callback(NULL, driver_desc, driver_name, context, 0);
407 for (adapter = 0; SUCCEEDED(hr) && cont_enum; adapter++)
409 char DriverName[512] = "";
411 /* The Battle.net System Checker expects the GetAdapterIdentifier DeviceName to match the
412 * Driver Name, so obtain the DeviceName and GUID from D3D. */
413 memset(&adapter_id, 0x0, sizeof(adapter_id));
414 adapter_id.device_name = DriverName;
415 adapter_id.device_name_size = sizeof(DriverName);
416 wined3d_mutex_lock();
417 hr = wined3d_get_adapter_identifier(wined3d, adapter, 0x0, &adapter_id);
418 wined3d_mutex_unlock();
419 if (SUCCEEDED(hr))
421 TRACE("Interface %d: %s\n", adapter, wine_dbgstr_guid(&adapter_id.device_identifier));
422 cont_enum = callback(&adapter_id.device_identifier, driver_desc,
423 adapter_id.device_name, context, 0);
427 __EXCEPT_PAGE_FAULT
429 wined3d_decref(wined3d);
430 return DDERR_INVALIDPARAMS;
432 __ENDTRY;
434 wined3d_decref(wined3d);
435 TRACE("End of enumeration\n");
436 return DD_OK;
439 /***********************************************************************
440 * DirectDrawEnumerateW (DDRAW.@)
442 * Enumerates legacy drivers, unicode version.
443 * This function is not implemented on Windows.
445 ***********************************************************************/
446 HRESULT WINAPI DirectDrawEnumerateW(LPDDENUMCALLBACKW callback, void *context)
448 TRACE("callback %p, context %p.\n", callback, context);
450 if (!callback)
451 return DDERR_INVALIDPARAMS;
452 else
453 return DDERR_UNSUPPORTED;
456 /***********************************************************************
457 * DirectDrawEnumerateExW (DDRAW.@)
459 * Enumerates DirectDraw7 drivers, unicode version.
460 * This function is not implemented on Windows.
462 ***********************************************************************/
463 HRESULT WINAPI DirectDrawEnumerateExW(LPDDENUMCALLBACKEXW callback, void *context, DWORD flags)
465 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
467 return DDERR_UNSUPPORTED;
470 /***********************************************************************
471 * Classfactory implementation.
472 ***********************************************************************/
474 /***********************************************************************
475 * CF_CreateDirectDraw
477 * DDraw 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_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
490 void **obj)
492 HRESULT hr;
494 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(iid), obj);
496 wined3d_mutex_lock();
497 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
498 wined3d_mutex_unlock();
500 return hr;
503 /***********************************************************************
504 * CF_CreateDirectDraw
506 * Clipper creation function for the class factory
508 * Params:
509 * UnkOuter: Set to NULL
510 * iid: ID of the wanted interface
511 * obj: Address to pass the interface pointer back
513 * Returns
514 * DD_OK / DDERR*, see DDRAW_Create
516 ***********************************************************************/
517 static HRESULT
518 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
519 void **obj)
521 HRESULT hr;
522 IDirectDrawClipper *Clip;
524 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(riid), obj);
526 wined3d_mutex_lock();
527 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
528 if (hr != DD_OK)
530 wined3d_mutex_unlock();
531 return hr;
534 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
535 IDirectDrawClipper_Release(Clip);
537 wined3d_mutex_unlock();
539 return hr;
542 static const struct object_creation_info object_creation[] =
544 { &CLSID_DirectDraw, CF_CreateDirectDraw },
545 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
546 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
549 struct ddraw_class_factory
551 IClassFactory IClassFactory_iface;
553 LONG ref;
554 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID iid, LPVOID *ppObj);
557 static inline struct ddraw_class_factory *impl_from_IClassFactory(IClassFactory *iface)
559 return CONTAINING_RECORD(iface, struct ddraw_class_factory, IClassFactory_iface);
562 /*******************************************************************************
563 * IDirectDrawClassFactory::QueryInterface
565 * QueryInterface for the class factory
567 * PARAMS
568 * riid Reference to identifier of queried interface
569 * ppv Address to return the interface pointer at
571 * RETURNS
572 * Success: S_OK
573 * Failure: E_NOINTERFACE
575 *******************************************************************************/
576 static HRESULT WINAPI ddraw_class_factory_QueryInterface(IClassFactory *iface, REFIID riid, void **out)
578 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
580 if (IsEqualGUID(riid, &IID_IUnknown)
581 || IsEqualGUID(riid, &IID_IClassFactory))
583 IClassFactory_AddRef(iface);
584 *out = iface;
585 return S_OK;
588 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
590 return E_NOINTERFACE;
593 /*******************************************************************************
594 * IDirectDrawClassFactory::AddRef
596 * AddRef for the class factory
598 * RETURNS
599 * The new refcount
601 *******************************************************************************/
602 static ULONG WINAPI ddraw_class_factory_AddRef(IClassFactory *iface)
604 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
605 ULONG ref = InterlockedIncrement(&factory->ref);
607 TRACE("%p increasing refcount to %u.\n", factory, ref);
609 return ref;
612 /*******************************************************************************
613 * IDirectDrawClassFactory::Release
615 * Release for the class factory. If the refcount falls to 0, the object
616 * is destroyed
618 * RETURNS
619 * The new refcount
621 *******************************************************************************/
622 static ULONG WINAPI ddraw_class_factory_Release(IClassFactory *iface)
624 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
625 ULONG ref = InterlockedDecrement(&factory->ref);
627 TRACE("%p decreasing refcount to %u.\n", factory, ref);
629 if (!ref)
630 HeapFree(GetProcessHeap(), 0, factory);
632 return ref;
636 /*******************************************************************************
637 * IDirectDrawClassFactory::CreateInstance
639 * What is this? Seems to create DirectDraw objects...
641 * Params
642 * The usual things???
644 * RETURNS
645 * ???
647 *******************************************************************************/
648 static HRESULT WINAPI ddraw_class_factory_CreateInstance(IClassFactory *iface,
649 IUnknown *outer_unknown, REFIID riid, void **out)
651 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
653 TRACE("iface %p, outer_unknown %p, riid %s, out %p.\n",
654 iface, outer_unknown, debugstr_guid(riid), out);
656 return factory->pfnCreateInstance(outer_unknown, riid, out);
659 /*******************************************************************************
660 * IDirectDrawClassFactory::LockServer
662 * What is this?
664 * Params
665 * ???
667 * RETURNS
668 * S_OK, because it's a stub
670 *******************************************************************************/
671 static HRESULT WINAPI ddraw_class_factory_LockServer(IClassFactory *iface, BOOL dolock)
673 FIXME("iface %p, dolock %#x stub!\n", iface, dolock);
675 return S_OK;
678 /*******************************************************************************
679 * The class factory VTable
680 *******************************************************************************/
681 static const IClassFactoryVtbl IClassFactory_Vtbl =
683 ddraw_class_factory_QueryInterface,
684 ddraw_class_factory_AddRef,
685 ddraw_class_factory_Release,
686 ddraw_class_factory_CreateInstance,
687 ddraw_class_factory_LockServer
690 /*******************************************************************************
691 * DllGetClassObject [DDRAW.@]
692 * Retrieves class object from a DLL object
694 * NOTES
695 * Docs say returns STDAPI
697 * PARAMS
698 * rclsid [I] CLSID for the class object
699 * riid [I] Reference to identifier of interface for class object
700 * ppv [O] Address of variable to receive interface pointer for riid
702 * RETURNS
703 * Success: S_OK
704 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
705 * E_UNEXPECTED
707 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
709 struct ddraw_class_factory *factory;
710 unsigned int i;
712 TRACE("rclsid %s, riid %s, object %p.\n",
713 debugstr_guid(rclsid), debugstr_guid(riid), ppv);
715 if (!IsEqualGUID(&IID_IClassFactory, riid)
716 && !IsEqualGUID(&IID_IUnknown, riid))
717 return E_NOINTERFACE;
719 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
721 if (IsEqualGUID(object_creation[i].clsid, rclsid))
722 break;
725 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
727 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
728 return CLASS_E_CLASSNOTAVAILABLE;
731 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
732 if (factory == NULL) return E_OUTOFMEMORY;
734 factory->IClassFactory_iface.lpVtbl = &IClassFactory_Vtbl;
735 factory->ref = 1;
737 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
739 *ppv = factory;
740 return S_OK;
744 /*******************************************************************************
745 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
747 * RETURNS
748 * Success: S_OK
749 * Failure: S_FALSE
751 HRESULT WINAPI DllCanUnloadNow(void)
753 TRACE("\n");
755 return S_FALSE;
759 /***********************************************************************
760 * DllRegisterServer (DDRAW.@)
762 HRESULT WINAPI DllRegisterServer(void)
764 return __wine_register_resources( instance );
767 /***********************************************************************
768 * DllUnregisterServer (DDRAW.@)
770 HRESULT WINAPI DllUnregisterServer(void)
772 return __wine_unregister_resources( instance );
775 /*******************************************************************************
776 * DestroyCallback
778 * Callback function for the EnumSurfaces call in DllMain.
779 * Dumps some surface info and releases the surface
781 * Params:
782 * surf: The enumerated surface
783 * desc: it's description
784 * context: Pointer to the ddraw impl
786 * Returns:
787 * DDENUMRET_OK;
788 *******************************************************************************/
789 static HRESULT WINAPI
790 DestroyCallback(IDirectDrawSurface7 *surf,
791 DDSURFACEDESC2 *desc,
792 void *context)
794 struct ddraw_surface *Impl = impl_from_IDirectDrawSurface7(surf);
795 ULONG ref7, ref4, ref3, ref2, ref1, gamma_count, iface_count;
797 ref7 = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
798 ref4 = Impl->ref4;
799 ref3 = Impl->ref3;
800 ref2 = Impl->ref2;
801 ref1 = Impl->ref1;
802 gamma_count = Impl->gamma_count;
804 WARN("Surface %p has an reference counts of 7: %u 4: %u 3: %u 2: %u 1: %u gamma: %u\n",
805 Impl, ref7, ref4, ref3, ref2, ref1, gamma_count);
807 /* Skip surfaces which are attached somewhere or which are
808 * part of a complex compound. They will get released when destroying
809 * the root
811 if( (!Impl->is_complex_root) || (Impl->first_attached != Impl) )
812 return DDENUMRET_OK;
814 /* Destroy the surface */
815 iface_count = ddraw_surface_release_iface(Impl);
816 while (iface_count) iface_count = ddraw_surface_release_iface(Impl);
818 return DDENUMRET_OK;
821 /***********************************************************************
822 * get_config_key
824 * Reads a config key from the registry. Taken from WineD3D
826 ***********************************************************************/
827 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
829 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
830 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
831 return ERROR_FILE_NOT_FOUND;
834 /***********************************************************************
835 * DllMain (DDRAW.0)
837 * Could be used to register DirectDraw drivers, if we have more than
838 * one. Also used to destroy any objects left at unload if the
839 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
841 ***********************************************************************/
842 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD reason, LPVOID reserved)
844 TRACE("(%p,%x,%p)\n", hInstDLL, reason, reserved);
845 switch (reason)
847 case DLL_PROCESS_ATTACH:
849 static HMODULE ddraw_self;
850 HKEY hkey = 0;
851 WNDCLASSA wc;
853 /* Register the window class. This is used to create a hidden window
854 * for D3D rendering, if the application didn't pass one. It can also
855 * be used for creating a device window from SetCooperativeLevel(). */
856 wc.style = CS_HREDRAW | CS_VREDRAW;
857 wc.lpfnWndProc = DefWindowProcA;
858 wc.cbClsExtra = 0;
859 wc.cbWndExtra = 0;
860 wc.hInstance = hInstDLL;
861 wc.hIcon = 0;
862 wc.hCursor = 0;
863 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
864 wc.lpszMenuName = NULL;
865 wc.lpszClassName = DDRAW_WINDOW_CLASS_NAME;
866 if (!RegisterClassA(&wc))
868 ERR("Failed to register ddraw window class, last error %#x.\n", GetLastError());
869 return FALSE;
872 /* On Windows one can force the refresh rate that DirectDraw uses by
873 * setting an override value in dxdiag. This is documented in KB315614
874 * (main article), KB230002, and KB217348. By comparing registry dumps
875 * before and after setting the override, we see that the override value
876 * is stored in HKLM\Software\Microsoft\DirectDraw\ForceRefreshRate as a
877 * DWORD that represents the refresh rate to force. We use this
878 * registry entry to modify the behavior of SetDisplayMode so that Wine
879 * users can override the refresh rate in a Windows-compatible way.
881 * dxdiag will not accept a refresh rate lower than 40 or higher than
882 * 120 so this value should be within that range. It is, of course,
883 * possible for a user to set the registry entry value directly so that
884 * assumption might not hold.
886 * There is no current mechanism for setting this value through the Wine
887 * GUI. It would be most appropriate to set this value through a dxdiag
888 * clone, but it may be sufficient to use winecfg.
890 * TODO: Create a mechanism for setting this value through the Wine GUI.
892 if ( !RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectDraw", &hkey ) )
894 DWORD type, data, size;
896 size = sizeof(data);
897 if (!RegQueryValueExA( hkey, "ForceRefreshRate", NULL, &type, (LPBYTE)&data, &size ) && type == REG_DWORD)
899 TRACE("ForceRefreshRate set; overriding refresh rate to %d Hz\n", data);
900 force_refresh_rate = data;
902 RegCloseKey( hkey );
905 /* Prevent the ddraw module from being unloaded. When switching to
906 * exclusive mode, we replace the window proc of the ddraw window. If
907 * an application would unload ddraw from the WM_DESTROY handler for
908 * that window, it would return to unmapped memory and die. Apparently
909 * this is supposed to work on Windows. We should probably use
910 * GET_MODULE_HANDLE_EX_FLAG_PIN for this, but that's not currently
911 * implemented. */
912 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const WCHAR *)&ddraw_self, &ddraw_self))
913 ERR("Failed to get own module handle.\n");
915 instance = hInstDLL;
916 DisableThreadLibraryCalls(hInstDLL);
917 break;
920 case DLL_PROCESS_DETACH:
921 if(!list_empty(&global_ddraw_list))
923 struct list *entry, *entry2;
924 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
926 /* We remove elements from this loop */
927 LIST_FOR_EACH_SAFE(entry, entry2, &global_ddraw_list)
929 struct ddraw *ddraw = LIST_ENTRY(entry, struct ddraw, ddraw_list_entry);
930 HRESULT hr;
931 DDSURFACEDESC2 desc;
932 int i;
934 WARN("DDraw %p has a refcount of %d\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref3 + ddraw->ref2 + ddraw->ref1);
936 /* Add references to each interface to avoid freeing them unexpectedly */
937 IDirectDraw_AddRef(&ddraw->IDirectDraw_iface);
938 IDirectDraw2_AddRef(&ddraw->IDirectDraw2_iface);
939 IDirectDraw4_AddRef(&ddraw->IDirectDraw4_iface);
940 IDirectDraw7_AddRef(&ddraw->IDirectDraw7_iface);
942 /* Does a D3D device exist? Destroy it
943 * TODO: Destroy all Vertex buffers, Lights, Materials
944 * and execute buffers too
946 if(ddraw->d3ddevice)
948 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
949 while(IDirect3DDevice7_Release(&ddraw->d3ddevice->IDirect3DDevice7_iface));
952 /* Destroy the swapchain after any 3D device. The 3D device
953 * cleanup code needs a swapchain. Specifically, it tries to
954 * set the current render target to the front buffer. */
955 if (ddraw->wined3d_swapchain)
956 ddraw_destroy_swapchain(ddraw);
958 /* Try to release the objects
959 * Do an EnumSurfaces to find any hanging surfaces
961 memset(&desc, 0, sizeof(desc));
962 desc.dwSize = sizeof(desc);
963 for(i = 0; i <= 1; i++)
965 hr = IDirectDraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, DDENUMSURFACES_ALL,
966 &desc, ddraw, DestroyCallback);
967 if(hr != D3D_OK)
968 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
971 if (!list_empty(&ddraw->surface_list))
972 ERR("DDraw %p still has surfaces attached.\n", ddraw);
974 /* Release all hanging references to destroy the objects. This
975 * restores the screen mode too
977 while(IDirectDraw_Release(&ddraw->IDirectDraw_iface));
978 while(IDirectDraw2_Release(&ddraw->IDirectDraw2_iface));
979 while(IDirectDraw4_Release(&ddraw->IDirectDraw4_iface));
980 while(IDirectDraw7_Release(&ddraw->IDirectDraw7_iface));
984 if (reserved) break;
985 UnregisterClassA(DDRAW_WINDOW_CLASS_NAME, hInstDLL);
988 return TRUE;