1 /* DirectDraw Base Functions
3 * Copyright 1997-1999 Marcus Meissner
4 * Copyright 1998 Lionel Ulmer (most of Direct3D stuff)
5 * Copyright 2000-2001 TransGaming Technologies Inc.
7 * This file contains the (internal) driver registration functions,
8 * driver enumeration APIs and DirectDraw creation functions.
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #define GLPRIVATE_NO_REDEFINE
28 #include "wine/port.h"
42 #include "wine/exception.h"
48 /* This for all the enumeration and creation of D3D-related objects */
49 #include "ddraw_private.h"
50 #include "wine/debug.h"
51 #include "wine/library.h"
53 #include "gl_private.h"
55 #undef GLPRIVATE_NO_REDEFINE
57 #define MAX_DDRAW_DRIVERS 3
58 static const ddraw_driver* DDRAW_drivers[MAX_DDRAW_DRIVERS];
59 static int DDRAW_num_drivers; /* = 0 */
60 static int DDRAW_default_driver;
62 void (*wine_tsx11_lock_ptr)(void) = NULL;
63 void (*wine_tsx11_unlock_ptr)(void) = NULL;
65 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
67 /* filter for page-fault exceptions */
68 static WINE_EXCEPTION_FILTER(page_fault)
70 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
71 return EXCEPTION_EXECUTE_HANDLER;
72 return EXCEPTION_CONTINUE_SEARCH;
75 /**********************************************************************/
80 } DirectDrawEnumerateProcData;
82 BOOL opengl_initialized = 0;
86 #include "opengl_private.h"
88 static void *gl_handle = NULL;
90 #define GL_API_FUNCTION(f) typeof(f) * p##f;
92 #undef GL_API_FUNCTION
95 #define SONAME_LIBGL "libGL.so"
98 static BOOL DDRAW_bind_to_opengl( void )
100 const char *glname = SONAME_LIBGL;
102 gl_handle = wine_dlopen(glname, RTLD_NOW, NULL, 0);
104 WARN("Wine cannot find the OpenGL graphics library (%s).\n",glname);
108 #define GL_API_FUNCTION(f) \
109 if((p##f = wine_dlsym(gl_handle, #f, NULL, 0)) == NULL) \
111 WARN("Can't find symbol %s\n", #f); \
112 goto sym_not_found; \
115 #undef GL_API_FUNCTION
117 /* And now calls the function to initialize the various fields for the rendering devices */
118 return d3ddevice_init_at_startup(gl_handle);
121 WARN("Wine cannot find certain functions that it needs inside the OpenGL\n"
122 "graphics library. To enable Wine to use OpenGL please upgrade\n"
123 "your OpenGL libraries\n");
124 wine_dlclose(gl_handle, NULL, 0);
129 #endif /* HAVE_OPENGL */
131 BOOL s3tc_initialized = 0;
133 static void *s3tc_handle = NULL;
135 FUNC_FETCH_2D_TEXEL_RGBA_DXT1 fetch_2d_texel_rgba_dxt1;
136 FUNC_FETCH_2D_TEXEL_RGBA_DXT3 fetch_2d_texel_rgba_dxt3;
137 FUNC_FETCH_2D_TEXEL_RGBA_DXT5 fetch_2d_texel_rgba_dxt5;
139 #ifndef SONAME_LIBTXC_DXTN
140 #define SONAME_LIBTXC_DXTN "libtxc_dxtn.so"
143 static BOOL DDRAW_bind_to_s3tc( void )
145 const char * const s3tcname = SONAME_LIBTXC_DXTN;
147 s3tc_handle = wine_dlopen(s3tcname, RTLD_NOW, NULL, 0);
149 TRACE("No S3TC software decompression library seems to be present (%s).\n",s3tcname);
152 TRACE("Found S3TC software decompression library (%s).\n",s3tcname);
154 #define API_FUNCTION(f) \
155 if((f = wine_dlsym(s3tc_handle, #f, NULL, 0)) == NULL) \
157 WARN("Can't find symbol %s\n", #f); \
158 goto sym_not_found; \
160 API_FUNCTION(fetch_2d_texel_rgba_dxt1);
161 API_FUNCTION(fetch_2d_texel_rgba_dxt3);
162 API_FUNCTION(fetch_2d_texel_rgba_dxt5);
168 WARN("Wine cannot find functions that are necessary for S3TC software decompression\n");
169 wine_dlclose(s3tc_handle, NULL, 0);
174 /*******************************************************************************
175 * DirectDrawEnumerateExA (DDRAW.@)
177 * Enumerates all DirectDraw devices installed on the system.
180 * lpCallback [I] DDEnumCallbackEx function to be called with a description of
181 * each enumerated HAL.
182 * lpContext [I] application-defined value to be passed to the callback.
183 * dwFlags [I] Specifies the enumeration scope. see msdn.
187 * Failure: DDERR_INVALIDPARAMS
189 HRESULT WINAPI DirectDrawEnumerateExA(
190 LPDDENUMCALLBACKEXA lpCallback, LPVOID lpContext, DWORD dwFlags)
194 TRACE("(%p,%p, %08lx)\n", lpCallback, lpContext, dwFlags);
196 if (TRACE_ON(ddraw)) {
198 if (dwFlags & DDENUM_ATTACHEDSECONDARYDEVICES)
199 TRACE("DDENUM_ATTACHEDSECONDARYDEVICES ");
200 if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES)
201 TRACE("DDENUM_DETACHEDSECONDARYDEVICES ");
202 if (dwFlags & DDENUM_NONDISPLAYDEVICES)
203 TRACE("DDENUM_NONDISPLAYDEVICES ");
207 for (i=0; i<DDRAW_num_drivers; i++)
209 TRACE("Enumerating %s/%s interface\n",
210 DDRAW_drivers[i]->info->szDriver,
211 DDRAW_drivers[i]->info->szDescription);
213 /* We have to pass NULL from the primary display device.
214 * RoadRage chapter 6's enumeration routine expects it. */
217 if (!lpCallback((DDRAW_default_driver == i) ? NULL
218 :(LPGUID)&DDRAW_drivers[i]->info->guidDeviceIdentifier,
219 (LPSTR)DDRAW_drivers[i]->info->szDescription,
220 (LPSTR)DDRAW_drivers[i]->info->szDriver,
229 if (stop) return DD_OK;
232 /* Unsupported flags */
233 if (dwFlags & DDENUM_NONDISPLAYDEVICES) {
234 FIXME("no non-display devices supported.\n");
236 if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES) {
237 FIXME("no detached secondary devices supported.\n");
243 /*******************************************************************************
244 * DirectDrawEnumerateExW (DDRAW.@)
247 static BOOL CALLBACK DirectDrawEnumerateExProcW(
248 GUID *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName,
249 LPVOID lpContext, HMONITOR hm)
253 LPWSTR lpDriverDescriptionW, lpDriverNameW;
254 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
256 len = MultiByteToWideChar( CP_ACP, 0, lpDriverDescription, -1, NULL, 0 );
257 lpDriverDescriptionW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
258 MultiByteToWideChar( CP_ACP, 0, lpDriverDescription, -1, lpDriverDescriptionW, len );
260 len = MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, NULL, 0 );
261 lpDriverNameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
262 MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, lpDriverNameW, len );
264 bResult = (*(LPDDENUMCALLBACKEXW *) pEPD->lpCallback)(lpGUID, lpDriverDescriptionW,
265 lpDriverNameW, pEPD->lpContext, hm);
267 HeapFree(GetProcessHeap(), 0, lpDriverDescriptionW);
268 HeapFree(GetProcessHeap(), 0, lpDriverNameW);
272 HRESULT WINAPI DirectDrawEnumerateExW(
273 LPDDENUMCALLBACKEXW lpCallback, LPVOID lpContext, DWORD dwFlags)
275 DirectDrawEnumerateProcData epd;
276 epd.lpCallback = (LPVOID) lpCallback;
277 epd.lpContext = lpContext;
279 return DirectDrawEnumerateExA(DirectDrawEnumerateExProcW, (LPVOID) &epd, 0);
282 /***********************************************************************
283 * DirectDrawEnumerateA (DDRAW.@)
286 static BOOL CALLBACK DirectDrawEnumerateProcA(
287 GUID *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName,
288 LPVOID lpContext, HMONITOR hm)
290 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
292 return ((LPDDENUMCALLBACKA) pEPD->lpCallback)(
293 lpGUID, lpDriverDescription, lpDriverName, pEPD->lpContext);
296 HRESULT WINAPI DirectDrawEnumerateA(
297 LPDDENUMCALLBACKA lpCallback, LPVOID lpContext)
299 DirectDrawEnumerateProcData epd;
300 epd.lpCallback = (LPVOID) lpCallback;
301 epd.lpContext = lpContext;
303 return DirectDrawEnumerateExA(DirectDrawEnumerateProcA, (LPVOID) &epd, 0);
306 /***********************************************************************
307 * DirectDrawEnumerateW (DDRAW.@)
310 static BOOL WINAPI DirectDrawEnumerateProcW(
311 GUID *lpGUID, LPWSTR lpDriverDescription, LPWSTR lpDriverName,
312 LPVOID lpContext, HMONITOR hm)
314 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
316 return ((LPDDENUMCALLBACKW) pEPD->lpCallback)(
317 lpGUID, lpDriverDescription, lpDriverName, pEPD->lpContext);
320 HRESULT WINAPI DirectDrawEnumerateW(
321 LPDDENUMCALLBACKW lpCallback, LPVOID lpContext)
323 DirectDrawEnumerateProcData epd;
324 epd.lpCallback = (LPVOID) lpCallback;
325 epd.lpContext = lpContext;
327 return DirectDrawEnumerateExW(DirectDrawEnumerateProcW, (LPVOID) &epd, 0);
330 /***********************************************************************
331 * DirectDrawCreate (DDRAW.@)
334 const ddraw_driver* DDRAW_FindDriver(const GUID* pGUID)
336 static const GUID zeroGUID; /* gets zero-inited */
338 TRACE("(%s)\n", pGUID ? debugstr_guid(pGUID) : "(null)");
340 if (DDRAW_num_drivers == 0) return NULL;
342 if (pGUID == (LPGUID)DDCREATE_EMULATIONONLY
343 || pGUID == (LPGUID)DDCREATE_HARDWAREONLY)
346 if (pGUID == NULL || memcmp(pGUID, &zeroGUID, sizeof(GUID)) == 0)
348 /* Use the default driver. */
349 return DDRAW_drivers[DDRAW_default_driver];
353 /* Look for a matching GUID. */
356 for (i=0; i < DDRAW_num_drivers; i++)
358 if (IsEqualGUID(pGUID,
359 &DDRAW_drivers[i]->info->guidDeviceIdentifier))
363 if (i < DDRAW_num_drivers)
365 return DDRAW_drivers[i];
369 ERR("(%s): did not recognize requested GUID.\n",debugstr_guid(pGUID));
375 static HRESULT DDRAW_Create(
376 LPGUID lpGUID, LPVOID *lplpDD, LPUNKNOWN pUnkOuter, REFIID iid, BOOL ex
378 const ddraw_driver* driver;
382 TRACE("(%s,%p,%p,%d)\n", debugstr_guid(lpGUID), lplpDD, pUnkOuter, ex);
384 if (DDRAW_num_drivers == 0)
386 WARN("no DirectDraw drivers registered\n");
387 return DDERR_INVALIDDIRECTDRAWGUID;
390 if (lpGUID == (LPGUID)DDCREATE_EMULATIONONLY
391 || lpGUID == (LPGUID)DDCREATE_HARDWAREONLY)
394 if (pUnkOuter != NULL)
395 return DDERR_INVALIDPARAMS; /* CLASS_E_NOAGGREGATION? */
397 driver = DDRAW_FindDriver(lpGUID);
398 if (driver == NULL) return DDERR_INVALIDDIRECTDRAWGUID;
400 hr = driver->create(lpGUID, &pDD, pUnkOuter, ex);
401 if (FAILED(hr)) return hr;
403 hr = IDirectDraw7_QueryInterface(pDD, iid, lplpDD);
404 IDirectDraw7_Release(pDD);
408 /***********************************************************************
409 * DirectDrawCreate (DDRAW.@)
411 * Only creates legacy IDirectDraw interfaces.
412 * Cannot create IDirectDraw7 interfaces.
415 HRESULT WINAPI DirectDrawCreate(
416 LPGUID lpGUID, LPDIRECTDRAW* lplpDD, LPUNKNOWN pUnkOuter
418 TRACE("(%s,%p,%p)\n", debugstr_guid(lpGUID), lplpDD, pUnkOuter);
419 return DDRAW_Create(lpGUID, (LPVOID*) lplpDD, pUnkOuter, &IID_IDirectDraw, FALSE);
422 /***********************************************************************
423 * DirectDrawCreateEx (DDRAW.@)
425 * Only creates new IDirectDraw7 interfaces.
426 * Supposed to fail if legacy interfaces are requested.
429 HRESULT WINAPI DirectDrawCreateEx(
430 LPGUID lpGUID, LPVOID* lplpDD, REFIID iid, LPUNKNOWN pUnkOuter
432 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(lpGUID), lplpDD, debugstr_guid(iid), pUnkOuter);
434 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
435 return DDERR_INVALIDPARAMS;
437 return DDRAW_Create(lpGUID, lplpDD, pUnkOuter, iid, TRUE);
440 extern HRESULT Uninit_DirectDraw_Create(const GUID*, LPDIRECTDRAW7*,
443 /* This is for the class factory. */
444 static HRESULT DDRAW_CreateDirectDraw(IUnknown* pUnkOuter, REFIID iid,
451 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppObj);
453 /* This is a mighty hack :-) */
454 if (IsEqualGUID(iid, &IID_IDirectDraw7))
459 hr = Uninit_DirectDraw_Create(NULL, &pDD, pUnkOuter, ex);
460 if (FAILED(hr)) return hr;
462 hr = IDirectDraw7_QueryInterface(pDD, iid, ppObj);
463 IDirectDraw_Release(pDD);
467 /******************************************************************************
468 * DirectDraw ClassFactory
471 ICOM_VFIELD_MULTI(IClassFactory);
474 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID iid,
478 struct object_creation_info
481 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID riid,
485 /* There should be more, but these are the only ones listed in the header
487 extern HRESULT DDRAW_CreateDirectDrawClipper(IUnknown *pUnkOuter, REFIID riid,
490 static const struct object_creation_info object_creation[] =
492 { &CLSID_DirectDraw, DDRAW_CreateDirectDraw },
493 { &CLSID_DirectDraw7, DDRAW_CreateDirectDraw },
494 { &CLSID_DirectDrawClipper, DDRAW_CreateDirectDrawClipper }
497 static HRESULT WINAPI
498 DDCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
500 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
502 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj);
504 if (IsEqualGUID(riid, &IID_IUnknown)
505 || IsEqualGUID(riid, &IID_IClassFactory))
507 IClassFactory_AddRef(iface);
512 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
513 return E_NOINTERFACE;
516 static ULONG WINAPI DDCF_AddRef(LPCLASSFACTORY iface)
518 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
519 ULONG ref = InterlockedIncrement(&This->ref);
521 TRACE("(%p)->() incrementing from %ld.\n", This, ref - 1);
526 static ULONG WINAPI DDCF_Release(LPCLASSFACTORY iface)
528 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
529 ULONG ref = InterlockedDecrement(&This->ref);
530 TRACE("(%p)->() decrementing from %ld.\n", This, ref+1);
533 HeapFree(GetProcessHeap(), 0, This);
539 static HRESULT WINAPI DDCF_CreateInstance(
540 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
543 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
545 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
547 return This->pfnCreateInstance(pOuter, riid, ppobj);
550 static HRESULT WINAPI DDCF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
552 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
553 FIXME("(%p)->(%d),stub!\n",This,dolock);
557 static const IClassFactoryVtbl DDCF_Vtbl =
566 /*******************************************************************************
567 * DllGetClassObject [DDRAW.@]
568 * Retrieves class object from a DLL object
571 * Docs say returns STDAPI
574 * rclsid [I] CLSID for the class object
575 * riid [I] Reference to identifier of interface for class object
576 * ppv [O] Address of variable to receive interface pointer for riid
580 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
583 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
586 IClassFactoryImpl *factory;
588 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
590 if ( !IsEqualGUID( &IID_IClassFactory, riid )
591 && ! IsEqualGUID( &IID_IUnknown, riid) )
592 return E_NOINTERFACE;
594 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
596 if (IsEqualGUID(object_creation[i].clsid, rclsid))
600 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
602 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
603 return CLASS_E_CLASSNOTAVAILABLE;
606 factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
607 if (factory == NULL) return E_OUTOFMEMORY;
609 ICOM_INIT_INTERFACE(factory, IClassFactory, DDCF_Vtbl);
612 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
614 *ppv = ICOM_INTERFACE(factory, IClassFactory);
619 /*******************************************************************************
620 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
626 HRESULT WINAPI DllCanUnloadNow(void)
628 FIXME("(void): stub\n");
632 /******************************************************************************
636 /* Choose which driver is considered the primary display driver. It will
637 * be created when we get a NULL guid for the DirectDrawCreate(Ex). */
638 static int DDRAW_ChooseDefaultDriver(void)
644 assert(DDRAW_num_drivers > 0);
646 /* This algorithm is really stupid. */
647 for (i=0; i < DDRAW_num_drivers; i++)
649 if (DDRAW_drivers[i]->preference > best_score)
651 best_score = DDRAW_drivers[i]->preference;
656 assert(best_score > 0);
661 /***********************************************************************
664 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
666 /* If we were sufficiently cool, DDraw drivers would just be COM
667 * objects, registered with a particular component category. */
669 DDRAW_HAL_Init(hInstDLL, fdwReason, lpv);
670 DDRAW_User_Init(hInstDLL, fdwReason, lpv);
672 if (fdwReason == DLL_PROCESS_ATTACH)
676 DisableThreadLibraryCalls(hInstDLL);
678 mod = GetModuleHandleA( "winex11.drv" );
681 wine_tsx11_lock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
682 wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
685 opengl_initialized = DDRAW_bind_to_opengl();
686 #endif /* HAVE_OPENGL */
687 s3tc_initialized = DDRAW_bind_to_s3tc();
690 if (DDRAW_num_drivers > 0)
691 DDRAW_default_driver = DDRAW_ChooseDefaultDriver();
696 /* Register a direct draw driver. This should be called from your init
697 * function. (That's why there is no locking: your init func is called from
698 * our DllInit, which is serialised.) */
699 void DDRAW_register_driver(const ddraw_driver *driver)
703 for (i = 0; i < DDRAW_num_drivers; i++)
705 if (DDRAW_drivers[i] == driver)
707 ERR("Driver reregistering %p\n", driver);
712 if (DDRAW_num_drivers == sizeof(DDRAW_drivers)/sizeof(DDRAW_drivers[0]))
714 ERR("too many DDRAW drivers\n");
718 DDRAW_drivers[DDRAW_num_drivers++] = driver;
721 /* This totally doesn't belong here. */
722 LONG DDRAW_width_bpp_to_pitch(DWORD width, DWORD bpp)
726 assert(bpp != 0); /* keeps happening... */
728 if (bpp == 15) bpp = 16;
729 pitch = width * (bpp / 8);
730 return pitch + (8 - (pitch % 8)) % 8;