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"
44 /* This for all the enumeration and creation of D3D-related objects */
45 #include "ddraw_private.h"
46 #include "wine/debug.h"
47 #include "wine/library.h"
49 #include "gl_private.h"
51 #undef GLPRIVATE_NO_REDEFINE
53 #define MAX_DDRAW_DRIVERS 3
54 static const ddraw_driver
* DDRAW_drivers
[MAX_DDRAW_DRIVERS
];
55 static int DDRAW_num_drivers
; /* = 0 */
56 static int DDRAW_default_driver
;
58 void (*wine_tsx11_lock_ptr
)(void) = NULL
;
59 void (*wine_tsx11_unlock_ptr
)(void) = NULL
;
61 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
63 /**********************************************************************/
68 } DirectDrawEnumerateProcData
;
70 BOOL opengl_initialized
= 0;
74 #include "mesa_private.h"
76 static void *gl_handle
= NULL
;
78 #define GL_API_FUNCTION(f) typeof(f) * p##f;
80 #undef GL_API_FUNCTION
83 #define SONAME_LIBGL "libGL.so"
86 static BOOL
DDRAW_bind_to_opengl( void )
88 const char *glname
= SONAME_LIBGL
;
90 gl_handle
= wine_dlopen(glname
, RTLD_NOW
, NULL
, 0);
92 WARN("Wine cannot find the OpenGL graphics library (%s).\n",glname
);
96 #define GL_API_FUNCTION(f) \
97 if((p##f = wine_dlsym(gl_handle, #f, NULL, 0)) == NULL) \
99 WARN("Can't find symbol %s\n", #f); \
100 goto sym_not_found; \
103 #undef GL_API_FUNCTION
105 /* And now calls the function to initialize the various fields for the rendering devices */
106 return d3ddevice_init_at_startup(gl_handle
);
109 WARN("Wine cannot find certain functions that it needs inside the OpenGL\n"
110 "graphics library. To enable Wine to use OpenGL please upgrade\n"
111 "your OpenGL libraries\n");
112 wine_dlclose(gl_handle
, NULL
, 0);
117 #endif /* HAVE_OPENGL */
119 BOOL s3tc_initialized
= 0;
121 static void *s3tc_handle
= NULL
;
123 FUNC_FETCH_2D_TEXEL_RGBA_DXT1 fetch_2d_texel_rgba_dxt1
;
124 FUNC_FETCH_2D_TEXEL_RGBA_DXT3 fetch_2d_texel_rgba_dxt3
;
125 FUNC_FETCH_2D_TEXEL_RGBA_DXT5 fetch_2d_texel_rgba_dxt5
;
127 #ifndef SONAME_LIBTXC_DXTN
128 #define SONAME_LIBTXC_DXTN "libtxc_dxtn.so"
131 static BOOL
DDRAW_bind_to_s3tc( void )
133 const char * const s3tcname
= SONAME_LIBTXC_DXTN
;
135 s3tc_handle
= wine_dlopen(s3tcname
, RTLD_NOW
, NULL
, 0);
137 TRACE("No S3TC software decompression library seems to be present (%s).\n",s3tcname
);
140 TRACE("Found S3TC software decompression library (%s).\n",s3tcname
);
142 #define API_FUNCTION(f) \
143 if((f = wine_dlsym(s3tc_handle, #f, NULL, 0)) == NULL) \
145 WARN("Can't find symbol %s\n", #f); \
146 goto sym_not_found; \
148 API_FUNCTION(fetch_2d_texel_rgba_dxt1
);
149 API_FUNCTION(fetch_2d_texel_rgba_dxt3
);
150 API_FUNCTION(fetch_2d_texel_rgba_dxt5
);
156 WARN("Wine cannot find functions that are necessary for S3TC software decompression\n");
157 wine_dlclose(s3tc_handle
, NULL
, 0);
162 /***********************************************************************
163 * DirectDrawEnumerateExA (DDRAW.@)
165 HRESULT WINAPI
DirectDrawEnumerateExA(
166 LPDDENUMCALLBACKEXA lpCallback
, LPVOID lpContext
, DWORD dwFlags
)
169 TRACE("(%p,%p, %08lx)\n", lpCallback
, lpContext
, dwFlags
);
171 if (TRACE_ON(ddraw
)) {
173 if (dwFlags
& DDENUM_ATTACHEDSECONDARYDEVICES
)
174 TRACE("DDENUM_ATTACHEDSECONDARYDEVICES ");
175 if (dwFlags
& DDENUM_DETACHEDSECONDARYDEVICES
)
176 TRACE("DDENUM_DETACHEDSECONDARYDEVICES ");
177 if (dwFlags
& DDENUM_NONDISPLAYDEVICES
)
178 TRACE("DDENUM_NONDISPLAYDEVICES ");
182 for (i
=0; i
<DDRAW_num_drivers
; i
++)
184 TRACE("Enumerating %s/%s interface\n",
185 DDRAW_drivers
[i
]->info
->szDriver
,
186 DDRAW_drivers
[i
]->info
->szDescription
);
188 /* We have to pass NULL from the primary display device.
189 * RoadRage chapter 6's enumeration routine expects it. */
190 if (!lpCallback((DDRAW_default_driver
== i
) ? NULL
191 :(LPGUID
)&DDRAW_drivers
[i
]->info
->guidDeviceIdentifier
,
192 (LPSTR
)DDRAW_drivers
[i
]->info
->szDescription
,
193 (LPSTR
)DDRAW_drivers
[i
]->info
->szDriver
,
198 /* Unsupported flags */
199 if (dwFlags
& DDENUM_NONDISPLAYDEVICES
) {
200 FIXME("no non-display devices supported.\n");
202 if (dwFlags
& DDENUM_DETACHEDSECONDARYDEVICES
) {
203 FIXME("no detached secondary devices supported.\n");
209 /***********************************************************************
210 * DirectDrawEnumerateExW (DDRAW.@)
213 static BOOL CALLBACK
DirectDrawEnumerateExProcW(
214 GUID
*lpGUID
, LPSTR lpDriverDescription
, LPSTR lpDriverName
,
215 LPVOID lpContext
, HMONITOR hm
)
219 LPWSTR lpDriverDescriptionW
, lpDriverNameW
;
220 DirectDrawEnumerateProcData
*pEPD
= (DirectDrawEnumerateProcData
*)lpContext
;
222 len
= MultiByteToWideChar( CP_ACP
, 0, lpDriverDescription
, -1, NULL
, 0 );
223 lpDriverDescriptionW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
224 MultiByteToWideChar( CP_ACP
, 0, lpDriverDescription
, -1, lpDriverDescriptionW
, len
);
226 len
= MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, NULL
, 0 );
227 lpDriverNameW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
228 MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, lpDriverNameW
, len
);
230 bResult
= (*(LPDDENUMCALLBACKEXW
*) pEPD
->lpCallback
)(lpGUID
, lpDriverDescriptionW
,
231 lpDriverNameW
, pEPD
->lpContext
, hm
);
233 HeapFree(GetProcessHeap(), 0, lpDriverDescriptionW
);
234 HeapFree(GetProcessHeap(), 0, lpDriverNameW
);
238 HRESULT WINAPI
DirectDrawEnumerateExW(
239 LPDDENUMCALLBACKEXW lpCallback
, LPVOID lpContext
, DWORD dwFlags
)
241 DirectDrawEnumerateProcData epd
;
242 epd
.lpCallback
= (LPVOID
) lpCallback
;
243 epd
.lpContext
= lpContext
;
245 return DirectDrawEnumerateExA(DirectDrawEnumerateExProcW
, (LPVOID
) &epd
, 0);
248 /***********************************************************************
249 * DirectDrawEnumerateA (DDRAW.@)
252 static BOOL CALLBACK
DirectDrawEnumerateProcA(
253 GUID
*lpGUID
, LPSTR lpDriverDescription
, LPSTR lpDriverName
,
254 LPVOID lpContext
, HMONITOR hm
)
256 DirectDrawEnumerateProcData
*pEPD
= (DirectDrawEnumerateProcData
*)lpContext
;
258 return ((LPDDENUMCALLBACKA
) pEPD
->lpCallback
)(
259 lpGUID
, lpDriverDescription
, lpDriverName
, pEPD
->lpContext
);
262 HRESULT WINAPI
DirectDrawEnumerateA(
263 LPDDENUMCALLBACKA lpCallback
, LPVOID lpContext
)
265 DirectDrawEnumerateProcData epd
;
266 epd
.lpCallback
= (LPVOID
) lpCallback
;
267 epd
.lpContext
= lpContext
;
269 return DirectDrawEnumerateExA(DirectDrawEnumerateProcA
, (LPVOID
) &epd
, 0);
272 /***********************************************************************
273 * DirectDrawEnumerateW (DDRAW.@)
276 static BOOL WINAPI
DirectDrawEnumerateProcW(
277 GUID
*lpGUID
, LPWSTR lpDriverDescription
, LPWSTR lpDriverName
,
278 LPVOID lpContext
, HMONITOR hm
)
280 DirectDrawEnumerateProcData
*pEPD
= (DirectDrawEnumerateProcData
*)lpContext
;
282 return ((LPDDENUMCALLBACKW
) pEPD
->lpCallback
)(
283 lpGUID
, lpDriverDescription
, lpDriverName
, pEPD
->lpContext
);
286 HRESULT WINAPI
DirectDrawEnumerateW(
287 LPDDENUMCALLBACKW lpCallback
, LPVOID lpContext
)
289 DirectDrawEnumerateProcData epd
;
290 epd
.lpCallback
= (LPVOID
) lpCallback
;
291 epd
.lpContext
= lpContext
;
293 return DirectDrawEnumerateExW(DirectDrawEnumerateProcW
, (LPVOID
) &epd
, 0);
296 /***********************************************************************
297 * DirectDrawCreate (DDRAW.@)
300 const ddraw_driver
* DDRAW_FindDriver(const GUID
* pGUID
)
302 static const GUID zeroGUID
; /* gets zero-inited */
304 TRACE("(%s)\n", pGUID
? debugstr_guid(pGUID
) : "(null)");
306 if (DDRAW_num_drivers
== 0) return NULL
;
308 if (pGUID
== (LPGUID
)DDCREATE_EMULATIONONLY
309 || pGUID
== (LPGUID
)DDCREATE_HARDWAREONLY
)
312 if (pGUID
== NULL
|| memcmp(pGUID
, &zeroGUID
, sizeof(GUID
)) == 0)
314 /* Use the default driver. */
315 return DDRAW_drivers
[DDRAW_default_driver
];
319 /* Look for a matching GUID. */
322 for (i
=0; i
< DDRAW_num_drivers
; i
++)
324 if (IsEqualGUID(pGUID
,
325 &DDRAW_drivers
[i
]->info
->guidDeviceIdentifier
))
329 if (i
< DDRAW_num_drivers
)
331 return DDRAW_drivers
[i
];
335 ERR("(%s): did not recognize requested GUID.\n",debugstr_guid(pGUID
));
341 static HRESULT
DDRAW_Create(
342 LPGUID lpGUID
, LPVOID
*lplpDD
, LPUNKNOWN pUnkOuter
, REFIID iid
, BOOL ex
344 const ddraw_driver
* driver
;
348 TRACE("(%s,%p,%p,%d)\n", debugstr_guid(lpGUID
), lplpDD
, pUnkOuter
, ex
);
350 if (DDRAW_num_drivers
== 0)
352 WARN("no DirectDraw drivers registered\n");
353 return DDERR_INVALIDDIRECTDRAWGUID
;
356 if (lpGUID
== (LPGUID
)DDCREATE_EMULATIONONLY
357 || lpGUID
== (LPGUID
)DDCREATE_HARDWAREONLY
)
360 if (pUnkOuter
!= NULL
)
361 return DDERR_INVALIDPARAMS
; /* CLASS_E_NOAGGREGATION? */
363 driver
= DDRAW_FindDriver(lpGUID
);
364 if (driver
== NULL
) return DDERR_INVALIDDIRECTDRAWGUID
;
366 hr
= driver
->create(lpGUID
, &pDD
, pUnkOuter
, ex
);
367 if (FAILED(hr
)) return hr
;
369 hr
= IDirectDraw7_QueryInterface(pDD
, iid
, lplpDD
);
370 IDirectDraw7_Release(pDD
);
374 /***********************************************************************
375 * DirectDrawCreate (DDRAW.@)
377 * Only creates legacy IDirectDraw interfaces.
378 * Cannot create IDirectDraw7 interfaces.
381 HRESULT WINAPI
DirectDrawCreate(
382 LPGUID lpGUID
, LPDIRECTDRAW
* lplpDD
, LPUNKNOWN pUnkOuter
384 TRACE("(%s,%p,%p)\n", debugstr_guid(lpGUID
), lplpDD
, pUnkOuter
);
385 return DDRAW_Create(lpGUID
, (LPVOID
*) lplpDD
, pUnkOuter
, &IID_IDirectDraw
, FALSE
);
388 /***********************************************************************
389 * DirectDrawCreateEx (DDRAW.@)
391 * Only creates new IDirectDraw7 interfaces.
392 * Supposed to fail if legacy interfaces are requested.
395 HRESULT WINAPI
DirectDrawCreateEx(
396 LPGUID lpGUID
, LPVOID
* lplpDD
, REFIID iid
, LPUNKNOWN pUnkOuter
398 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(lpGUID
), lplpDD
, debugstr_guid(iid
), pUnkOuter
);
400 if (!IsEqualGUID(iid
, &IID_IDirectDraw7
))
401 return DDERR_INVALIDPARAMS
;
403 return DDRAW_Create(lpGUID
, lplpDD
, pUnkOuter
, iid
, TRUE
);
406 extern HRESULT
Uninit_DirectDraw_Create(const GUID
*, LPDIRECTDRAW7
*,
409 /* This is for the class factory. */
410 static HRESULT
DDRAW_CreateDirectDraw(IUnknown
* pUnkOuter
, REFIID iid
,
417 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppObj
);
419 /* This is a mighty hack :-) */
420 if (IsEqualGUID(iid
, &IID_IDirectDraw7
))
425 hr
= Uninit_DirectDraw_Create(NULL
, &pDD
, pUnkOuter
, ex
);
426 if (FAILED(hr
)) return hr
;
428 hr
= IDirectDraw7_QueryInterface(pDD
, iid
, ppObj
);
429 IDirectDraw_Release(pDD
);
433 /******************************************************************************
434 * DirectDraw ClassFactory
437 ICOM_VFIELD_MULTI(IClassFactory
);
440 HRESULT (*pfnCreateInstance
)(IUnknown
*pUnkOuter
, REFIID iid
,
444 struct object_creation_info
447 HRESULT (*pfnCreateInstance
)(IUnknown
*pUnkOuter
, REFIID riid
,
451 /* There should be more, but these are the only ones listed in the header
453 extern HRESULT
DDRAW_CreateDirectDrawClipper(IUnknown
*pUnkOuter
, REFIID riid
,
456 static const struct object_creation_info object_creation
[] =
458 { &CLSID_DirectDraw
, DDRAW_CreateDirectDraw
},
459 { &CLSID_DirectDraw7
, DDRAW_CreateDirectDraw
},
460 { &CLSID_DirectDrawClipper
, DDRAW_CreateDirectDrawClipper
}
463 static HRESULT WINAPI
464 DDCF_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
,LPVOID
*ppobj
)
466 ICOM_THIS(IClassFactoryImpl
,iface
);
468 TRACE("(%p)->(%s,%p)\n", This
, debugstr_guid(riid
), ppobj
);
470 if (IsEqualGUID(riid
, &IID_IUnknown
)
471 || IsEqualGUID(riid
, &IID_IClassFactory
))
473 IClassFactory_AddRef(iface
);
478 WARN("(%p)->(%s,%p),not found\n",This
,debugstr_guid(riid
),ppobj
);
479 return E_NOINTERFACE
;
482 static ULONG WINAPI
DDCF_AddRef(LPCLASSFACTORY iface
) {
483 ICOM_THIS(IClassFactoryImpl
,iface
);
485 TRACE("(%p)->() incrementing from %ld.\n", This
, This
->ref
);
487 return ++(This
->ref
);
490 static ULONG WINAPI
DDCF_Release(LPCLASSFACTORY iface
) {
491 ICOM_THIS(IClassFactoryImpl
,iface
);
493 TRACE("(%p)->() decrementing from %ld.\n", This
, This
->ref
);
495 if (--This
->ref
== 0)
496 HeapFree(GetProcessHeap(), 0, This
);
502 static HRESULT WINAPI
DDCF_CreateInstance(
503 LPCLASSFACTORY iface
,LPUNKNOWN pOuter
,REFIID riid
,LPVOID
*ppobj
505 ICOM_THIS(IClassFactoryImpl
,iface
);
507 TRACE("(%p)->(%p,%s,%p)\n",This
,pOuter
,debugstr_guid(riid
),ppobj
);
509 return This
->pfnCreateInstance(pOuter
, riid
, ppobj
);
512 static HRESULT WINAPI
DDCF_LockServer(LPCLASSFACTORY iface
,BOOL dolock
) {
513 ICOM_THIS(IClassFactoryImpl
,iface
);
514 FIXME("(%p)->(%d),stub!\n",This
,dolock
);
518 static ICOM_VTABLE(IClassFactory
) DDCF_Vtbl
=
520 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
528 /*******************************************************************************
529 * DllGetClassObject [DDRAW.@]
530 * Retrieves class object from a DLL object
533 * Docs say returns STDAPI
536 * rclsid [I] CLSID for the class object
537 * riid [I] Reference to identifier of interface for class object
538 * ppv [O] Address of variable to receive interface pointer for riid
542 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
545 DWORD WINAPI
DDRAW_DllGetClassObject(REFCLSID rclsid
,REFIID riid
,LPVOID
*ppv
)
548 IClassFactoryImpl
*factory
;
550 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
552 if ( !IsEqualGUID( &IID_IClassFactory
, riid
)
553 && ! IsEqualGUID( &IID_IUnknown
, riid
) )
554 return E_NOINTERFACE
;
556 for (i
=0; i
< sizeof(object_creation
)/sizeof(object_creation
[0]); i
++)
558 if (IsEqualGUID(object_creation
[i
].clsid
, rclsid
))
562 if (i
== sizeof(object_creation
)/sizeof(object_creation
[0]))
564 FIXME("%s: no class found.\n", debugstr_guid(rclsid
));
565 return CLASS_E_CLASSNOTAVAILABLE
;
568 factory
= HeapAlloc(GetProcessHeap(), 0, sizeof(*factory
));
569 if (factory
== NULL
) return E_OUTOFMEMORY
;
571 ICOM_INIT_INTERFACE(factory
, IClassFactory
, DDCF_Vtbl
);
574 factory
->pfnCreateInstance
= object_creation
[i
].pfnCreateInstance
;
576 *ppv
= ICOM_INTERFACE(factory
, IClassFactory
);
581 /*******************************************************************************
582 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
588 DWORD WINAPI
DDRAW_DllCanUnloadNow(void) {
589 FIXME("(void): stub\n");
593 /******************************************************************************
597 /* Choose which driver is considered the primary display driver. It will
598 * be created when we get a NULL guid for the DirectDrawCreate(Ex). */
599 static int DDRAW_ChooseDefaultDriver(void)
605 assert(DDRAW_num_drivers
> 0);
607 /* This algorithm is really stupid. */
608 for (i
=0; i
< DDRAW_num_drivers
; i
++)
610 if (DDRAW_drivers
[i
]->preference
> best_score
)
612 best_score
= DDRAW_drivers
[i
]->preference
;
617 assert(best_score
> 0);
622 /***********************************************************************
625 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpv
)
627 /* If we were sufficiently cool, DDraw drivers would just be COM
628 * objects, registered with a particular component category. */
630 DDRAW_HAL_Init(hInstDLL
, fdwReason
, lpv
);
631 DDRAW_User_Init(hInstDLL
, fdwReason
, lpv
);
633 if (fdwReason
== DLL_PROCESS_ATTACH
)
637 DisableThreadLibraryCalls(hInstDLL
);
639 mod
= GetModuleHandleA( "x11drv.dll" );
642 wine_tsx11_lock_ptr
= (void *)GetProcAddress( mod
, "wine_tsx11_lock" );
643 wine_tsx11_unlock_ptr
= (void *)GetProcAddress( mod
, "wine_tsx11_unlock" );
646 opengl_initialized
= DDRAW_bind_to_opengl();
647 #endif /* HAVE_OPENGL */
648 s3tc_initialized
= DDRAW_bind_to_s3tc();
651 if (DDRAW_num_drivers
> 0)
652 DDRAW_default_driver
= DDRAW_ChooseDefaultDriver();
657 /* Register a direct draw driver. This should be called from your init
658 * function. (That's why there is no locking: your init func is called from
659 * our DllInit, which is serialised.) */
660 void DDRAW_register_driver(const ddraw_driver
*driver
)
664 for (i
= 0; i
< DDRAW_num_drivers
; i
++)
666 if (DDRAW_drivers
[i
] == driver
)
668 ERR("Driver reregistering %p\n", driver
);
673 if (DDRAW_num_drivers
== sizeof(DDRAW_drivers
)/sizeof(DDRAW_drivers
[0]))
675 ERR("too many DDRAW drivers\n");
679 DDRAW_drivers
[DDRAW_num_drivers
++] = driver
;
682 /* This totally doesn't belong here. */
683 LONG
DDRAW_width_bpp_to_pitch(DWORD width
, DWORD bpp
)
687 assert(bpp
!= 0); /* keeps happening... */
689 if (bpp
== 15) bpp
= 16;
690 pitch
= width
* (bpp
/ 8);
691 return pitch
+ (8 - (pitch
% 8)) % 8;