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"
46 /* This for all the enumeration and creation of D3D-related objects */
47 #include "ddraw_private.h"
48 #include "wine/debug.h"
49 #include "wine/library.h"
51 #include "gl_private.h"
53 #undef GLPRIVATE_NO_REDEFINE
55 #define MAX_DDRAW_DRIVERS 3
56 static const ddraw_driver* DDRAW_drivers[MAX_DDRAW_DRIVERS];
57 static int DDRAW_num_drivers; /* = 0 */
58 static int DDRAW_default_driver;
60 void (*wine_tsx11_lock_ptr)(void) = NULL;
61 void (*wine_tsx11_unlock_ptr)(void) = NULL;
63 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
65 /**********************************************************************/
70 } DirectDrawEnumerateProcData;
72 BOOL opengl_initialized = 0;
76 #include "opengl_private.h"
78 static void *gl_handle = NULL;
80 #define GL_API_FUNCTION(f) typeof(f) * p##f;
82 #undef GL_API_FUNCTION
85 #define SONAME_LIBGL "libGL.so"
88 static BOOL DDRAW_bind_to_opengl( void )
90 const char *glname = SONAME_LIBGL;
92 gl_handle = wine_dlopen(glname, RTLD_NOW, NULL, 0);
94 WARN("Wine cannot find the OpenGL graphics library (%s).\n",glname);
98 #define GL_API_FUNCTION(f) \
99 if((p##f = wine_dlsym(gl_handle, #f, NULL, 0)) == NULL) \
101 WARN("Can't find symbol %s\n", #f); \
102 goto sym_not_found; \
105 #undef GL_API_FUNCTION
107 /* And now calls the function to initialize the various fields for the rendering devices */
108 return d3ddevice_init_at_startup(gl_handle);
111 WARN("Wine cannot find certain functions that it needs inside the OpenGL\n"
112 "graphics library. To enable Wine to use OpenGL please upgrade\n"
113 "your OpenGL libraries\n");
114 wine_dlclose(gl_handle, NULL, 0);
119 #endif /* HAVE_OPENGL */
121 BOOL s3tc_initialized = 0;
123 static void *s3tc_handle = NULL;
125 FUNC_FETCH_2D_TEXEL_RGBA_DXT1 fetch_2d_texel_rgba_dxt1;
126 FUNC_FETCH_2D_TEXEL_RGBA_DXT3 fetch_2d_texel_rgba_dxt3;
127 FUNC_FETCH_2D_TEXEL_RGBA_DXT5 fetch_2d_texel_rgba_dxt5;
129 #ifndef SONAME_LIBTXC_DXTN
130 #define SONAME_LIBTXC_DXTN "libtxc_dxtn.so"
133 static BOOL DDRAW_bind_to_s3tc( void )
135 const char * const s3tcname = SONAME_LIBTXC_DXTN;
137 s3tc_handle = wine_dlopen(s3tcname, RTLD_NOW, NULL, 0);
139 TRACE("No S3TC software decompression library seems to be present (%s).\n",s3tcname);
142 TRACE("Found S3TC software decompression library (%s).\n",s3tcname);
144 #define API_FUNCTION(f) \
145 if((f = wine_dlsym(s3tc_handle, #f, NULL, 0)) == NULL) \
147 WARN("Can't find symbol %s\n", #f); \
148 goto sym_not_found; \
150 API_FUNCTION(fetch_2d_texel_rgba_dxt1);
151 API_FUNCTION(fetch_2d_texel_rgba_dxt3);
152 API_FUNCTION(fetch_2d_texel_rgba_dxt5);
158 WARN("Wine cannot find functions that are necessary for S3TC software decompression\n");
159 wine_dlclose(s3tc_handle, NULL, 0);
164 /*******************************************************************************
165 * DirectDrawEnumerateExA (DDRAW.@)
167 * Enumerates all DirectDraw devices installed on the system.
170 * lpCallback [I] DDEnumCallbackEx function to be called with a description of
171 * each enumerated HAL.
172 * lpContext [I] application-defined value to be passed to the callback.
173 * dwFlags [I] Specifies the enumeration scope. see msdn.
177 * Failure: DDERR_INVALIDPARAMS
179 HRESULT WINAPI DirectDrawEnumerateExA(
180 LPDDENUMCALLBACKEXA lpCallback, LPVOID lpContext, DWORD dwFlags)
183 TRACE("(%p,%p, %08lx)\n", lpCallback, lpContext, dwFlags);
185 if (TRACE_ON(ddraw)) {
187 if (dwFlags & DDENUM_ATTACHEDSECONDARYDEVICES)
188 TRACE("DDENUM_ATTACHEDSECONDARYDEVICES ");
189 if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES)
190 TRACE("DDENUM_DETACHEDSECONDARYDEVICES ");
191 if (dwFlags & DDENUM_NONDISPLAYDEVICES)
192 TRACE("DDENUM_NONDISPLAYDEVICES ");
196 for (i=0; i<DDRAW_num_drivers; i++)
198 TRACE("Enumerating %s/%s interface\n",
199 DDRAW_drivers[i]->info->szDriver,
200 DDRAW_drivers[i]->info->szDescription);
202 /* We have to pass NULL from the primary display device.
203 * RoadRage chapter 6's enumeration routine expects it. */
204 if (!lpCallback((DDRAW_default_driver == i) ? NULL
205 :(LPGUID)&DDRAW_drivers[i]->info->guidDeviceIdentifier,
206 (LPSTR)DDRAW_drivers[i]->info->szDescription,
207 (LPSTR)DDRAW_drivers[i]->info->szDriver,
212 /* Unsupported flags */
213 if (dwFlags & DDENUM_NONDISPLAYDEVICES) {
214 FIXME("no non-display devices supported.\n");
216 if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES) {
217 FIXME("no detached secondary devices supported.\n");
223 /*******************************************************************************
224 * DirectDrawEnumerateExW (DDRAW.@)
227 static BOOL CALLBACK DirectDrawEnumerateExProcW(
228 GUID *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName,
229 LPVOID lpContext, HMONITOR hm)
233 LPWSTR lpDriverDescriptionW, lpDriverNameW;
234 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
236 len = MultiByteToWideChar( CP_ACP, 0, lpDriverDescription, -1, NULL, 0 );
237 lpDriverDescriptionW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
238 MultiByteToWideChar( CP_ACP, 0, lpDriverDescription, -1, lpDriverDescriptionW, len );
240 len = MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, NULL, 0 );
241 lpDriverNameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
242 MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, lpDriverNameW, len );
244 bResult = (*(LPDDENUMCALLBACKEXW *) pEPD->lpCallback)(lpGUID, lpDriverDescriptionW,
245 lpDriverNameW, pEPD->lpContext, hm);
247 HeapFree(GetProcessHeap(), 0, lpDriverDescriptionW);
248 HeapFree(GetProcessHeap(), 0, lpDriverNameW);
252 HRESULT WINAPI DirectDrawEnumerateExW(
253 LPDDENUMCALLBACKEXW lpCallback, LPVOID lpContext, DWORD dwFlags)
255 DirectDrawEnumerateProcData epd;
256 epd.lpCallback = (LPVOID) lpCallback;
257 epd.lpContext = lpContext;
259 return DirectDrawEnumerateExA(DirectDrawEnumerateExProcW, (LPVOID) &epd, 0);
262 /***********************************************************************
263 * DirectDrawEnumerateA (DDRAW.@)
266 static BOOL CALLBACK DirectDrawEnumerateProcA(
267 GUID *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName,
268 LPVOID lpContext, HMONITOR hm)
270 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
272 return ((LPDDENUMCALLBACKA) pEPD->lpCallback)(
273 lpGUID, lpDriverDescription, lpDriverName, pEPD->lpContext);
276 HRESULT WINAPI DirectDrawEnumerateA(
277 LPDDENUMCALLBACKA lpCallback, LPVOID lpContext)
279 DirectDrawEnumerateProcData epd;
280 epd.lpCallback = (LPVOID) lpCallback;
281 epd.lpContext = lpContext;
283 return DirectDrawEnumerateExA(DirectDrawEnumerateProcA, (LPVOID) &epd, 0);
286 /***********************************************************************
287 * DirectDrawEnumerateW (DDRAW.@)
290 static BOOL WINAPI DirectDrawEnumerateProcW(
291 GUID *lpGUID, LPWSTR lpDriverDescription, LPWSTR lpDriverName,
292 LPVOID lpContext, HMONITOR hm)
294 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
296 return ((LPDDENUMCALLBACKW) pEPD->lpCallback)(
297 lpGUID, lpDriverDescription, lpDriverName, pEPD->lpContext);
300 HRESULT WINAPI DirectDrawEnumerateW(
301 LPDDENUMCALLBACKW lpCallback, LPVOID lpContext)
303 DirectDrawEnumerateProcData epd;
304 epd.lpCallback = (LPVOID) lpCallback;
305 epd.lpContext = lpContext;
307 return DirectDrawEnumerateExW(DirectDrawEnumerateProcW, (LPVOID) &epd, 0);
310 /***********************************************************************
311 * DirectDrawCreate (DDRAW.@)
314 const ddraw_driver* DDRAW_FindDriver(const GUID* pGUID)
316 static const GUID zeroGUID; /* gets zero-inited */
318 TRACE("(%s)\n", pGUID ? debugstr_guid(pGUID) : "(null)");
320 if (DDRAW_num_drivers == 0) return NULL;
322 if (pGUID == (LPGUID)DDCREATE_EMULATIONONLY
323 || pGUID == (LPGUID)DDCREATE_HARDWAREONLY)
326 if (pGUID == NULL || memcmp(pGUID, &zeroGUID, sizeof(GUID)) == 0)
328 /* Use the default driver. */
329 return DDRAW_drivers[DDRAW_default_driver];
333 /* Look for a matching GUID. */
336 for (i=0; i < DDRAW_num_drivers; i++)
338 if (IsEqualGUID(pGUID,
339 &DDRAW_drivers[i]->info->guidDeviceIdentifier))
343 if (i < DDRAW_num_drivers)
345 return DDRAW_drivers[i];
349 ERR("(%s): did not recognize requested GUID.\n",debugstr_guid(pGUID));
355 static HRESULT DDRAW_Create(
356 LPGUID lpGUID, LPVOID *lplpDD, LPUNKNOWN pUnkOuter, REFIID iid, BOOL ex
358 const ddraw_driver* driver;
362 TRACE("(%s,%p,%p,%d)\n", debugstr_guid(lpGUID), lplpDD, pUnkOuter, ex);
364 if (DDRAW_num_drivers == 0)
366 WARN("no DirectDraw drivers registered\n");
367 return DDERR_INVALIDDIRECTDRAWGUID;
370 if (lpGUID == (LPGUID)DDCREATE_EMULATIONONLY
371 || lpGUID == (LPGUID)DDCREATE_HARDWAREONLY)
374 if (pUnkOuter != NULL)
375 return DDERR_INVALIDPARAMS; /* CLASS_E_NOAGGREGATION? */
377 driver = DDRAW_FindDriver(lpGUID);
378 if (driver == NULL) return DDERR_INVALIDDIRECTDRAWGUID;
380 hr = driver->create(lpGUID, &pDD, pUnkOuter, ex);
381 if (FAILED(hr)) return hr;
383 hr = IDirectDraw7_QueryInterface(pDD, iid, lplpDD);
384 IDirectDraw7_Release(pDD);
388 /***********************************************************************
389 * DirectDrawCreate (DDRAW.@)
391 * Only creates legacy IDirectDraw interfaces.
392 * Cannot create IDirectDraw7 interfaces.
395 HRESULT WINAPI DirectDrawCreate(
396 LPGUID lpGUID, LPDIRECTDRAW* lplpDD, LPUNKNOWN pUnkOuter
398 TRACE("(%s,%p,%p)\n", debugstr_guid(lpGUID), lplpDD, pUnkOuter);
399 return DDRAW_Create(lpGUID, (LPVOID*) lplpDD, pUnkOuter, &IID_IDirectDraw, FALSE);
402 /***********************************************************************
403 * DirectDrawCreateEx (DDRAW.@)
405 * Only creates new IDirectDraw7 interfaces.
406 * Supposed to fail if legacy interfaces are requested.
409 HRESULT WINAPI DirectDrawCreateEx(
410 LPGUID lpGUID, LPVOID* lplpDD, REFIID iid, LPUNKNOWN pUnkOuter
412 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(lpGUID), lplpDD, debugstr_guid(iid), pUnkOuter);
414 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
415 return DDERR_INVALIDPARAMS;
417 return DDRAW_Create(lpGUID, lplpDD, pUnkOuter, iid, TRUE);
420 extern HRESULT Uninit_DirectDraw_Create(const GUID*, LPDIRECTDRAW7*,
423 /* This is for the class factory. */
424 static HRESULT DDRAW_CreateDirectDraw(IUnknown* pUnkOuter, REFIID iid,
431 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppObj);
433 /* This is a mighty hack :-) */
434 if (IsEqualGUID(iid, &IID_IDirectDraw7))
439 hr = Uninit_DirectDraw_Create(NULL, &pDD, pUnkOuter, ex);
440 if (FAILED(hr)) return hr;
442 hr = IDirectDraw7_QueryInterface(pDD, iid, ppObj);
443 IDirectDraw_Release(pDD);
447 /******************************************************************************
448 * DirectDraw ClassFactory
451 ICOM_VFIELD_MULTI(IClassFactory);
454 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID iid,
458 struct object_creation_info
461 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID riid,
465 /* There should be more, but these are the only ones listed in the header
467 extern HRESULT DDRAW_CreateDirectDrawClipper(IUnknown *pUnkOuter, REFIID riid,
470 static const struct object_creation_info object_creation[] =
472 { &CLSID_DirectDraw, DDRAW_CreateDirectDraw },
473 { &CLSID_DirectDraw7, DDRAW_CreateDirectDraw },
474 { &CLSID_DirectDrawClipper, DDRAW_CreateDirectDrawClipper }
477 static HRESULT WINAPI
478 DDCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
480 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
482 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj);
484 if (IsEqualGUID(riid, &IID_IUnknown)
485 || IsEqualGUID(riid, &IID_IClassFactory))
487 IClassFactory_AddRef(iface);
492 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
493 return E_NOINTERFACE;
496 static ULONG WINAPI DDCF_AddRef(LPCLASSFACTORY iface)
498 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
499 ULONG ref = InterlockedIncrement(&This->ref);
501 TRACE("(%p)->() incrementing from %ld.\n", This, ref - 1);
506 static ULONG WINAPI DDCF_Release(LPCLASSFACTORY iface)
508 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
509 ULONG ref = InterlockedDecrement(&This->ref);
510 TRACE("(%p)->() decrementing from %ld.\n", This, ref+1);
513 HeapFree(GetProcessHeap(), 0, This);
519 static HRESULT WINAPI DDCF_CreateInstance(
520 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
523 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
525 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
527 return This->pfnCreateInstance(pOuter, riid, ppobj);
530 static HRESULT WINAPI DDCF_LockServer(LPCLASSFACTORY iface,BOOL dolock)
532 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
533 FIXME("(%p)->(%d),stub!\n",This,dolock);
537 static const IClassFactoryVtbl DDCF_Vtbl =
546 /*******************************************************************************
547 * DllGetClassObject [DDRAW.@]
548 * Retrieves class object from a DLL object
551 * Docs say returns STDAPI
554 * rclsid [I] CLSID for the class object
555 * riid [I] Reference to identifier of interface for class object
556 * ppv [O] Address of variable to receive interface pointer for riid
560 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
563 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
566 IClassFactoryImpl *factory;
568 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
570 if ( !IsEqualGUID( &IID_IClassFactory, riid )
571 && ! IsEqualGUID( &IID_IUnknown, riid) )
572 return E_NOINTERFACE;
574 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
576 if (IsEqualGUID(object_creation[i].clsid, rclsid))
580 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
582 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
583 return CLASS_E_CLASSNOTAVAILABLE;
586 factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
587 if (factory == NULL) return E_OUTOFMEMORY;
589 ICOM_INIT_INTERFACE(factory, IClassFactory, DDCF_Vtbl);
592 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
594 *ppv = ICOM_INTERFACE(factory, IClassFactory);
599 /*******************************************************************************
600 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
606 HRESULT WINAPI DllCanUnloadNow(void)
608 FIXME("(void): stub\n");
612 /******************************************************************************
616 /* Choose which driver is considered the primary display driver. It will
617 * be created when we get a NULL guid for the DirectDrawCreate(Ex). */
618 static int DDRAW_ChooseDefaultDriver(void)
624 assert(DDRAW_num_drivers > 0);
626 /* This algorithm is really stupid. */
627 for (i=0; i < DDRAW_num_drivers; i++)
629 if (DDRAW_drivers[i]->preference > best_score)
631 best_score = DDRAW_drivers[i]->preference;
636 assert(best_score > 0);
641 /***********************************************************************
644 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
646 /* If we were sufficiently cool, DDraw drivers would just be COM
647 * objects, registered with a particular component category. */
649 DDRAW_HAL_Init(hInstDLL, fdwReason, lpv);
650 DDRAW_User_Init(hInstDLL, fdwReason, lpv);
652 if (fdwReason == DLL_PROCESS_ATTACH)
656 DisableThreadLibraryCalls(hInstDLL);
658 mod = GetModuleHandleA( "winex11.drv" );
661 wine_tsx11_lock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
662 wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
665 opengl_initialized = DDRAW_bind_to_opengl();
666 #endif /* HAVE_OPENGL */
667 s3tc_initialized = DDRAW_bind_to_s3tc();
670 if (DDRAW_num_drivers > 0)
671 DDRAW_default_driver = DDRAW_ChooseDefaultDriver();
676 /* Register a direct draw driver. This should be called from your init
677 * function. (That's why there is no locking: your init func is called from
678 * our DllInit, which is serialised.) */
679 void DDRAW_register_driver(const ddraw_driver *driver)
683 for (i = 0; i < DDRAW_num_drivers; i++)
685 if (DDRAW_drivers[i] == driver)
687 ERR("Driver reregistering %p\n", driver);
692 if (DDRAW_num_drivers == sizeof(DDRAW_drivers)/sizeof(DDRAW_drivers[0]))
694 ERR("too many DDRAW drivers\n");
698 DDRAW_drivers[DDRAW_num_drivers++] = driver;
701 /* This totally doesn't belong here. */
702 LONG DDRAW_width_bpp_to_pitch(DWORD width, DWORD bpp)
706 assert(bpp != 0); /* keeps happening... */
708 if (bpp == 15) bpp = 16;
709 pitch = width * (bpp / 8);
710 return pitch + (8 - (pitch % 8)) % 8;