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 /**********************************************************************/
72 } DirectDrawEnumerateProcData
;
74 BOOL opengl_initialized
= 0;
78 #include "opengl_private.h"
80 static void *gl_handle
= NULL
;
82 #define GL_API_FUNCTION(f) typeof(f) * p##f;
84 #undef GL_API_FUNCTION
87 #define SONAME_LIBGL "libGL.so"
90 static BOOL
DDRAW_bind_to_opengl( void )
92 const char *glname
= SONAME_LIBGL
;
94 gl_handle
= wine_dlopen(glname
, RTLD_NOW
, NULL
, 0);
96 WARN("Wine cannot find the OpenGL graphics library (%s).\n",glname
);
100 #define GL_API_FUNCTION(f) \
101 if((p##f = wine_dlsym(gl_handle, #f, NULL, 0)) == NULL) \
103 WARN("Can't find symbol %s\n", #f); \
104 goto sym_not_found; \
107 #undef GL_API_FUNCTION
109 /* And now calls the function to initialize the various fields for the rendering devices */
110 return d3ddevice_init_at_startup(gl_handle
);
113 WARN("Wine cannot find certain functions that it needs inside the OpenGL\n"
114 "graphics library. To enable Wine to use OpenGL please upgrade\n"
115 "your OpenGL libraries\n");
116 wine_dlclose(gl_handle
, NULL
, 0);
121 #endif /* HAVE_OPENGL */
123 BOOL s3tc_initialized
= 0;
125 static void *s3tc_handle
= NULL
;
127 FUNC_FETCH_2D_TEXEL_RGBA_DXT1 fetch_2d_texel_rgba_dxt1
;
128 FUNC_FETCH_2D_TEXEL_RGBA_DXT3 fetch_2d_texel_rgba_dxt3
;
129 FUNC_FETCH_2D_TEXEL_RGBA_DXT5 fetch_2d_texel_rgba_dxt5
;
131 #ifndef SONAME_LIBTXC_DXTN
132 #define SONAME_LIBTXC_DXTN "libtxc_dxtn.so"
135 static BOOL
DDRAW_bind_to_s3tc( void )
137 const char * const s3tcname
= SONAME_LIBTXC_DXTN
;
139 s3tc_handle
= wine_dlopen(s3tcname
, RTLD_NOW
, NULL
, 0);
141 TRACE("No S3TC software decompression library seems to be present (%s).\n",s3tcname
);
144 TRACE("Found S3TC software decompression library (%s).\n",s3tcname
);
146 #define API_FUNCTION(f) \
147 if((f = wine_dlsym(s3tc_handle, #f, NULL, 0)) == NULL) \
149 WARN("Can't find symbol %s\n", #f); \
150 goto sym_not_found; \
152 API_FUNCTION(fetch_2d_texel_rgba_dxt1
);
153 API_FUNCTION(fetch_2d_texel_rgba_dxt3
);
154 API_FUNCTION(fetch_2d_texel_rgba_dxt5
);
160 WARN("Wine cannot find functions that are necessary for S3TC software decompression\n");
161 wine_dlclose(s3tc_handle
, NULL
, 0);
166 /*******************************************************************************
167 * DirectDrawEnumerateExA (DDRAW.@)
169 * Enumerates all DirectDraw devices installed on the system.
172 * lpCallback [I] DDEnumCallbackEx function to be called with a description of
173 * each enumerated HAL.
174 * lpContext [I] application-defined value to be passed to the callback.
175 * dwFlags [I] Specifies the enumeration scope. see msdn.
179 * Failure: DDERR_INVALIDPARAMS
181 HRESULT WINAPI
DirectDrawEnumerateExA(
182 LPDDENUMCALLBACKEXA lpCallback
, LPVOID lpContext
, DWORD dwFlags
)
186 TRACE("(%p,%p, %08lx)\n", lpCallback
, lpContext
, dwFlags
);
188 if (TRACE_ON(ddraw
)) {
190 if (dwFlags
& DDENUM_ATTACHEDSECONDARYDEVICES
)
191 TRACE("DDENUM_ATTACHEDSECONDARYDEVICES ");
192 if (dwFlags
& DDENUM_DETACHEDSECONDARYDEVICES
)
193 TRACE("DDENUM_DETACHEDSECONDARYDEVICES ");
194 if (dwFlags
& DDENUM_NONDISPLAYDEVICES
)
195 TRACE("DDENUM_NONDISPLAYDEVICES ");
199 for (i
=0; i
<DDRAW_num_drivers
; i
++)
201 TRACE("Enumerating %s/%s interface\n",
202 DDRAW_drivers
[i
]->info
->szDriver
,
203 DDRAW_drivers
[i
]->info
->szDescription
);
205 /* We have to pass NULL from the primary display device.
206 * RoadRage chapter 6's enumeration routine expects it. */
209 if (!lpCallback((DDRAW_default_driver
== i
) ? NULL
210 :(LPGUID
)&DDRAW_drivers
[i
]->info
->guidDeviceIdentifier
,
211 (LPSTR
)DDRAW_drivers
[i
]->info
->szDescription
,
212 (LPSTR
)DDRAW_drivers
[i
]->info
->szDriver
,
221 if (stop
) return DD_OK
;
224 /* Unsupported flags */
225 if (dwFlags
& DDENUM_NONDISPLAYDEVICES
) {
226 FIXME("no non-display devices supported.\n");
228 if (dwFlags
& DDENUM_DETACHEDSECONDARYDEVICES
) {
229 FIXME("no detached secondary devices supported.\n");
235 /*******************************************************************************
236 * DirectDrawEnumerateExW (DDRAW.@)
239 static BOOL CALLBACK
DirectDrawEnumerateExProcW(
240 GUID
*lpGUID
, LPSTR lpDriverDescription
, LPSTR lpDriverName
,
241 LPVOID lpContext
, HMONITOR hm
)
245 LPWSTR lpDriverDescriptionW
, lpDriverNameW
;
246 DirectDrawEnumerateProcData
*pEPD
= (DirectDrawEnumerateProcData
*)lpContext
;
248 len
= MultiByteToWideChar( CP_ACP
, 0, lpDriverDescription
, -1, NULL
, 0 );
249 lpDriverDescriptionW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
250 MultiByteToWideChar( CP_ACP
, 0, lpDriverDescription
, -1, lpDriverDescriptionW
, len
);
252 len
= MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, NULL
, 0 );
253 lpDriverNameW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
254 MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, lpDriverNameW
, len
);
256 bResult
= (*(LPDDENUMCALLBACKEXW
*) pEPD
->lpCallback
)(lpGUID
, lpDriverDescriptionW
,
257 lpDriverNameW
, pEPD
->lpContext
, hm
);
259 HeapFree(GetProcessHeap(), 0, lpDriverDescriptionW
);
260 HeapFree(GetProcessHeap(), 0, lpDriverNameW
);
264 HRESULT WINAPI
DirectDrawEnumerateExW(
265 LPDDENUMCALLBACKEXW lpCallback
, LPVOID lpContext
, DWORD dwFlags
)
267 DirectDrawEnumerateProcData epd
;
268 epd
.lpCallback
= (LPVOID
) lpCallback
;
269 epd
.lpContext
= lpContext
;
271 return DirectDrawEnumerateExA(DirectDrawEnumerateExProcW
, (LPVOID
) &epd
, 0);
274 /***********************************************************************
275 * DirectDrawEnumerateA (DDRAW.@)
278 static BOOL CALLBACK
DirectDrawEnumerateProcA(
279 GUID
*lpGUID
, LPSTR lpDriverDescription
, LPSTR lpDriverName
,
280 LPVOID lpContext
, HMONITOR hm
)
282 DirectDrawEnumerateProcData
*pEPD
= (DirectDrawEnumerateProcData
*)lpContext
;
284 return ((LPDDENUMCALLBACKA
) pEPD
->lpCallback
)(
285 lpGUID
, lpDriverDescription
, lpDriverName
, pEPD
->lpContext
);
288 HRESULT WINAPI
DirectDrawEnumerateA(
289 LPDDENUMCALLBACKA lpCallback
, LPVOID lpContext
)
291 DirectDrawEnumerateProcData epd
;
292 epd
.lpCallback
= (LPVOID
) lpCallback
;
293 epd
.lpContext
= lpContext
;
295 return DirectDrawEnumerateExA(DirectDrawEnumerateProcA
, (LPVOID
) &epd
, 0);
298 /***********************************************************************
299 * DirectDrawEnumerateW (DDRAW.@)
302 static BOOL WINAPI
DirectDrawEnumerateProcW(
303 GUID
*lpGUID
, LPWSTR lpDriverDescription
, LPWSTR lpDriverName
,
304 LPVOID lpContext
, HMONITOR hm
)
306 DirectDrawEnumerateProcData
*pEPD
= (DirectDrawEnumerateProcData
*)lpContext
;
308 return ((LPDDENUMCALLBACKW
) pEPD
->lpCallback
)(
309 lpGUID
, lpDriverDescription
, lpDriverName
, pEPD
->lpContext
);
312 HRESULT WINAPI
DirectDrawEnumerateW(
313 LPDDENUMCALLBACKW lpCallback
, LPVOID lpContext
)
315 DirectDrawEnumerateProcData epd
;
316 epd
.lpCallback
= (LPVOID
) lpCallback
;
317 epd
.lpContext
= lpContext
;
319 return DirectDrawEnumerateExW(DirectDrawEnumerateProcW
, (LPVOID
) &epd
, 0);
322 /***********************************************************************
323 * DirectDrawCreate (DDRAW.@)
326 const ddraw_driver
* DDRAW_FindDriver(const GUID
* pGUID
)
328 static const GUID zeroGUID
; /* gets zero-inited */
330 TRACE("(%s)\n", pGUID
? debugstr_guid(pGUID
) : "(null)");
332 if (DDRAW_num_drivers
== 0) return NULL
;
334 if (pGUID
== (LPGUID
)DDCREATE_EMULATIONONLY
335 || pGUID
== (LPGUID
)DDCREATE_HARDWAREONLY
)
338 if (pGUID
== NULL
|| memcmp(pGUID
, &zeroGUID
, sizeof(GUID
)) == 0)
340 /* Use the default driver. */
341 return DDRAW_drivers
[DDRAW_default_driver
];
345 /* Look for a matching GUID. */
348 for (i
=0; i
< DDRAW_num_drivers
; i
++)
350 if (IsEqualGUID(pGUID
,
351 &DDRAW_drivers
[i
]->info
->guidDeviceIdentifier
))
355 if (i
< DDRAW_num_drivers
)
357 return DDRAW_drivers
[i
];
361 ERR("(%s): did not recognize requested GUID.\n",debugstr_guid(pGUID
));
367 static HRESULT
DDRAW_Create(
368 LPGUID lpGUID
, LPVOID
*lplpDD
, LPUNKNOWN pUnkOuter
, REFIID iid
, BOOL ex
370 const ddraw_driver
* driver
;
374 TRACE("(%s,%p,%p,%d)\n", debugstr_guid(lpGUID
), lplpDD
, pUnkOuter
, ex
);
376 if (DDRAW_num_drivers
== 0)
378 WARN("no DirectDraw drivers registered\n");
379 return DDERR_INVALIDDIRECTDRAWGUID
;
382 if (lpGUID
== (LPGUID
)DDCREATE_EMULATIONONLY
383 || lpGUID
== (LPGUID
)DDCREATE_HARDWAREONLY
)
386 if (pUnkOuter
!= NULL
)
387 return DDERR_INVALIDPARAMS
; /* CLASS_E_NOAGGREGATION? */
389 driver
= DDRAW_FindDriver(lpGUID
);
390 if (driver
== NULL
) return DDERR_INVALIDDIRECTDRAWGUID
;
392 hr
= driver
->create(lpGUID
, &pDD
, pUnkOuter
, ex
);
393 if (FAILED(hr
)) return hr
;
395 hr
= IDirectDraw7_QueryInterface(pDD
, iid
, lplpDD
);
396 IDirectDraw7_Release(pDD
);
400 /***********************************************************************
401 * DirectDrawCreate (DDRAW.@)
403 * Only creates legacy IDirectDraw interfaces.
404 * Cannot create IDirectDraw7 interfaces.
407 HRESULT WINAPI
DirectDrawCreate(
408 LPGUID lpGUID
, LPDIRECTDRAW
* lplpDD
, LPUNKNOWN pUnkOuter
410 TRACE("(%s,%p,%p)\n", debugstr_guid(lpGUID
), lplpDD
, pUnkOuter
);
411 return DDRAW_Create(lpGUID
, (LPVOID
*) lplpDD
, pUnkOuter
, &IID_IDirectDraw
, FALSE
);
414 /***********************************************************************
415 * DirectDrawCreateEx (DDRAW.@)
417 * Only creates new IDirectDraw7 interfaces.
418 * Supposed to fail if legacy interfaces are requested.
421 HRESULT WINAPI
DirectDrawCreateEx(
422 LPGUID lpGUID
, LPVOID
* lplpDD
, REFIID iid
, LPUNKNOWN pUnkOuter
424 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(lpGUID
), lplpDD
, debugstr_guid(iid
), pUnkOuter
);
426 if (!IsEqualGUID(iid
, &IID_IDirectDraw7
))
427 return DDERR_INVALIDPARAMS
;
429 return DDRAW_Create(lpGUID
, lplpDD
, pUnkOuter
, iid
, TRUE
);
432 extern HRESULT
Uninit_DirectDraw_Create(const GUID
*, LPDIRECTDRAW7
*,
435 /* This is for the class factory. */
436 static HRESULT
DDRAW_CreateDirectDraw(IUnknown
* pUnkOuter
, REFIID iid
,
443 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppObj
);
445 /* This is a mighty hack :-) */
446 if (IsEqualGUID(iid
, &IID_IDirectDraw7
))
451 hr
= Uninit_DirectDraw_Create(NULL
, &pDD
, pUnkOuter
, ex
);
452 if (FAILED(hr
)) return hr
;
454 hr
= IDirectDraw7_QueryInterface(pDD
, iid
, ppObj
);
455 IDirectDraw_Release(pDD
);
459 /******************************************************************************
460 * DirectDraw ClassFactory
463 ICOM_VFIELD_MULTI(IClassFactory
);
466 HRESULT (*pfnCreateInstance
)(IUnknown
*pUnkOuter
, REFIID iid
,
470 struct object_creation_info
473 HRESULT (*pfnCreateInstance
)(IUnknown
*pUnkOuter
, REFIID riid
,
477 /* There should be more, but these are the only ones listed in the header
479 extern HRESULT
DDRAW_CreateDirectDrawClipper(IUnknown
*pUnkOuter
, REFIID riid
,
482 static const struct object_creation_info object_creation
[] =
484 { &CLSID_DirectDraw
, DDRAW_CreateDirectDraw
},
485 { &CLSID_DirectDraw7
, DDRAW_CreateDirectDraw
},
486 { &CLSID_DirectDrawClipper
, DDRAW_CreateDirectDrawClipper
}
489 static HRESULT WINAPI
490 DDCF_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
,LPVOID
*ppobj
)
492 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
494 TRACE("(%p)->(%s,%p)\n", This
, debugstr_guid(riid
), ppobj
);
496 if (IsEqualGUID(riid
, &IID_IUnknown
)
497 || IsEqualGUID(riid
, &IID_IClassFactory
))
499 IClassFactory_AddRef(iface
);
504 WARN("(%p)->(%s,%p),not found\n",This
,debugstr_guid(riid
),ppobj
);
505 return E_NOINTERFACE
;
508 static ULONG WINAPI
DDCF_AddRef(LPCLASSFACTORY iface
)
510 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
511 ULONG ref
= InterlockedIncrement(&This
->ref
);
513 TRACE("(%p)->() incrementing from %ld.\n", This
, ref
- 1);
518 static ULONG WINAPI
DDCF_Release(LPCLASSFACTORY iface
)
520 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
521 ULONG ref
= InterlockedDecrement(&This
->ref
);
522 TRACE("(%p)->() decrementing from %ld.\n", This
, ref
+1);
525 HeapFree(GetProcessHeap(), 0, This
);
531 static HRESULT WINAPI
DDCF_CreateInstance(
532 LPCLASSFACTORY iface
,LPUNKNOWN pOuter
,REFIID riid
,LPVOID
*ppobj
535 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
537 TRACE("(%p)->(%p,%s,%p)\n",This
,pOuter
,debugstr_guid(riid
),ppobj
);
539 return This
->pfnCreateInstance(pOuter
, riid
, ppobj
);
542 static HRESULT WINAPI
DDCF_LockServer(LPCLASSFACTORY iface
,BOOL dolock
)
544 IClassFactoryImpl
*This
= (IClassFactoryImpl
*)iface
;
545 FIXME("(%p)->(%d),stub!\n",This
,dolock
);
549 static const IClassFactoryVtbl DDCF_Vtbl
=
558 /*******************************************************************************
559 * DllGetClassObject [DDRAW.@]
560 * Retrieves class object from a DLL object
563 * Docs say returns STDAPI
566 * rclsid [I] CLSID for the class object
567 * riid [I] Reference to identifier of interface for class object
568 * ppv [O] Address of variable to receive interface pointer for riid
572 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
575 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
578 IClassFactoryImpl
*factory
;
580 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
582 if ( !IsEqualGUID( &IID_IClassFactory
, riid
)
583 && ! IsEqualGUID( &IID_IUnknown
, riid
) )
584 return E_NOINTERFACE
;
586 for (i
=0; i
< sizeof(object_creation
)/sizeof(object_creation
[0]); i
++)
588 if (IsEqualGUID(object_creation
[i
].clsid
, rclsid
))
592 if (i
== sizeof(object_creation
)/sizeof(object_creation
[0]))
594 FIXME("%s: no class found.\n", debugstr_guid(rclsid
));
595 return CLASS_E_CLASSNOTAVAILABLE
;
598 factory
= HeapAlloc(GetProcessHeap(), 0, sizeof(*factory
));
599 if (factory
== NULL
) return E_OUTOFMEMORY
;
601 ICOM_INIT_INTERFACE(factory
, IClassFactory
, DDCF_Vtbl
);
604 factory
->pfnCreateInstance
= object_creation
[i
].pfnCreateInstance
;
606 *ppv
= ICOM_INTERFACE(factory
, IClassFactory
);
611 /*******************************************************************************
612 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
618 HRESULT WINAPI
DllCanUnloadNow(void)
620 FIXME("(void): stub\n");
624 /******************************************************************************
628 /* Choose which driver is considered the primary display driver. It will
629 * be created when we get a NULL guid for the DirectDrawCreate(Ex). */
630 static int DDRAW_ChooseDefaultDriver(void)
636 assert(DDRAW_num_drivers
> 0);
638 /* This algorithm is really stupid. */
639 for (i
=0; i
< DDRAW_num_drivers
; i
++)
641 if (DDRAW_drivers
[i
]->preference
> best_score
)
643 best_score
= DDRAW_drivers
[i
]->preference
;
648 assert(best_score
> 0);
653 /***********************************************************************
656 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpv
)
658 /* If we were sufficiently cool, DDraw drivers would just be COM
659 * objects, registered with a particular component category. */
661 DDRAW_HAL_Init(hInstDLL
, fdwReason
, lpv
);
662 DDRAW_User_Init(hInstDLL
, fdwReason
, lpv
);
664 if (fdwReason
== DLL_PROCESS_ATTACH
)
668 DisableThreadLibraryCalls(hInstDLL
);
670 mod
= GetModuleHandleA( "winex11.drv" );
673 wine_tsx11_lock_ptr
= (void *)GetProcAddress( mod
, "wine_tsx11_lock" );
674 wine_tsx11_unlock_ptr
= (void *)GetProcAddress( mod
, "wine_tsx11_unlock" );
677 opengl_initialized
= DDRAW_bind_to_opengl();
678 #endif /* HAVE_OPENGL */
679 s3tc_initialized
= DDRAW_bind_to_s3tc();
681 if (DDRAW_num_drivers
> 0)
682 DDRAW_default_driver
= DDRAW_ChooseDefaultDriver();
688 /* Register a direct draw driver. This should be called from your init
689 * function. (That's why there is no locking: your init func is called from
690 * our DllInit, which is serialised.) */
691 void DDRAW_register_driver(const ddraw_driver
*driver
)
695 for (i
= 0; i
< DDRAW_num_drivers
; i
++)
697 if (DDRAW_drivers
[i
] == driver
)
699 ERR("Driver reregistering %p\n", driver
);
704 if (DDRAW_num_drivers
== sizeof(DDRAW_drivers
)/sizeof(DDRAW_drivers
[0]))
706 ERR("too many DDRAW drivers\n");
710 DDRAW_drivers
[DDRAW_num_drivers
++] = driver
;
713 /* This totally doesn't belong here. */
714 LONG
DDRAW_width_bpp_to_pitch(DWORD width
, DWORD bpp
)
718 assert(bpp
!= 0); /* keeps happening... */
720 if (bpp
== 15) bpp
= 16;
721 pitch
= width
* (bpp
/ 8);
722 return pitch
+ (8 - (pitch
% 8)) % 8;