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
39 /* This for all the enumeration and creation of D3D-related objects */
40 #include "ddraw_private.h"
41 #include "wine/debug.h"
42 #include "wine/library.h"
43 #include "wine/port.h"
45 #include "gl_private.h"
47 #undef GLPRIVATE_NO_REDEFINE
49 #define MAX_DDRAW_DRIVERS 3
50 static const ddraw_driver
* DDRAW_drivers
[MAX_DDRAW_DRIVERS
];
51 static int DDRAW_num_drivers
; /* = 0 */
52 static int DDRAW_default_driver
;
54 void (*wine_tsx11_lock_ptr
)(void) = NULL
;
55 void (*wine_tsx11_unlock_ptr
)(void) = NULL
;
57 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
59 /**********************************************************************/
64 } DirectDrawEnumerateProcData
;
66 BOOL opengl_initialized
= 0;
70 #include "mesa_private.h"
72 static void *gl_handle
= NULL
;
74 #define GL_API_FUNCTION(f) typeof(f) * p##f;
76 #undef GL_API_FUNCTION
79 #define SONAME_LIBGL "libGL.so"
82 static BOOL
DDRAW_bind_to_opengl( void )
84 char *glname
= SONAME_LIBGL
;
87 gl_handle
= wine_dlopen(glname
, RTLD_NOW
, NULL
, 0);
89 WARN("Wine cannot find the OpenGL graphics library (%s).\n",glname
);
93 #define GL_API_FUNCTION(f) \
94 if((p##f = wine_dlsym(gl_handle, #f, NULL, 0)) == NULL) \
96 WARN("Can't find symbol %s\n", #f); \
100 #undef GL_API_FUNCTION
102 /* And now calls the function to initialize the various fields for the rendering devices */
103 ret_value
= d3ddevice_init_at_startup(gl_handle
);
105 wine_dlclose(gl_handle
, NULL
, 0);
110 WARN("Wine cannot find certain functions that it needs inside the OpenGL\n"
111 "graphics library. To enable Wine to use OpenGL please upgrade\n"
112 "your OpenGL libraries\n");
113 wine_dlclose(gl_handle
, NULL
, 0);
118 #endif /* HAVE_OPENGL */
120 /***********************************************************************
121 * DirectDrawEnumerateExA (DDRAW.@)
123 HRESULT WINAPI
DirectDrawEnumerateExA(
124 LPDDENUMCALLBACKEXA lpCallback
, LPVOID lpContext
, DWORD dwFlags
)
127 TRACE("(%p,%p, %08lx)\n", lpCallback
, lpContext
, dwFlags
);
129 if (TRACE_ON(ddraw
)) {
131 if (dwFlags
& DDENUM_ATTACHEDSECONDARYDEVICES
)
132 TRACE("DDENUM_ATTACHEDSECONDARYDEVICES ");
133 if (dwFlags
& DDENUM_DETACHEDSECONDARYDEVICES
)
134 TRACE("DDENUM_DETACHEDSECONDARYDEVICES ");
135 if (dwFlags
& DDENUM_NONDISPLAYDEVICES
)
136 TRACE("DDENUM_NONDISPLAYDEVICES ");
140 for (i
=0; i
<DDRAW_num_drivers
; i
++)
142 TRACE("Enumerating %s/%s interface\n",
143 DDRAW_drivers
[i
]->info
->szDriver
,
144 DDRAW_drivers
[i
]->info
->szDescription
);
146 /* We have to pass NULL from the primary display device.
147 * RoadRage chapter 6's enumeration routine expects it. */
148 if (!lpCallback((DDRAW_default_driver
== i
) ? NULL
149 :(LPGUID
)&DDRAW_drivers
[i
]->info
->guidDeviceIdentifier
,
150 (LPSTR
)DDRAW_drivers
[i
]->info
->szDescription
,
151 (LPSTR
)DDRAW_drivers
[i
]->info
->szDriver
,
156 /* Unsupported flags */
157 if (dwFlags
& DDENUM_NONDISPLAYDEVICES
) {
158 FIXME("no non-display devices supported.\n");
160 if (dwFlags
& DDENUM_DETACHEDSECONDARYDEVICES
) {
161 FIXME("no detached secondary devices supported.\n");
167 /***********************************************************************
168 * DirectDrawEnumerateExW (DDRAW.@)
171 static BOOL CALLBACK
DirectDrawEnumerateExProcW(
172 GUID
*lpGUID
, LPSTR lpDriverDescription
, LPSTR lpDriverName
,
173 LPVOID lpContext
, HMONITOR hm
)
177 LPWSTR lpDriverDescriptionW
, lpDriverNameW
;
178 DirectDrawEnumerateProcData
*pEPD
= (DirectDrawEnumerateProcData
*)lpContext
;
180 len
= MultiByteToWideChar( CP_ACP
, 0, lpDriverDescription
, -1, NULL
, 0 );
181 lpDriverDescriptionW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
182 MultiByteToWideChar( CP_ACP
, 0, lpDriverDescription
, -1, lpDriverDescriptionW
, len
);
184 len
= MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, NULL
, 0 );
185 lpDriverNameW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
186 MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, lpDriverNameW
, len
);
188 bResult
= (*(LPDDENUMCALLBACKEXW
*) pEPD
->lpCallback
)(lpGUID
, lpDriverDescriptionW
,
189 lpDriverNameW
, pEPD
->lpContext
, hm
);
191 HeapFree(GetProcessHeap(), 0, lpDriverDescriptionW
);
192 HeapFree(GetProcessHeap(), 0, lpDriverNameW
);
196 HRESULT WINAPI
DirectDrawEnumerateExW(
197 LPDDENUMCALLBACKEXW lpCallback
, LPVOID lpContext
, DWORD dwFlags
)
199 DirectDrawEnumerateProcData epd
;
200 epd
.lpCallback
= (LPVOID
) lpCallback
;
201 epd
.lpContext
= lpContext
;
203 return DirectDrawEnumerateExA(DirectDrawEnumerateExProcW
, (LPVOID
) &epd
, 0);
206 /***********************************************************************
207 * DirectDrawEnumerateA (DDRAW.@)
210 static BOOL CALLBACK
DirectDrawEnumerateProcA(
211 GUID
*lpGUID
, LPSTR lpDriverDescription
, LPSTR lpDriverName
,
212 LPVOID lpContext
, HMONITOR hm
)
214 DirectDrawEnumerateProcData
*pEPD
= (DirectDrawEnumerateProcData
*)lpContext
;
216 return ((LPDDENUMCALLBACKA
) pEPD
->lpCallback
)(
217 lpGUID
, lpDriverDescription
, lpDriverName
, pEPD
->lpContext
);
220 HRESULT WINAPI
DirectDrawEnumerateA(
221 LPDDENUMCALLBACKA lpCallback
, LPVOID lpContext
)
223 DirectDrawEnumerateProcData epd
;
224 epd
.lpCallback
= (LPVOID
) lpCallback
;
225 epd
.lpContext
= lpContext
;
227 return DirectDrawEnumerateExA(DirectDrawEnumerateProcA
, (LPVOID
) &epd
, 0);
230 /***********************************************************************
231 * DirectDrawEnumerateW (DDRAW.@)
234 static BOOL WINAPI
DirectDrawEnumerateProcW(
235 GUID
*lpGUID
, LPWSTR lpDriverDescription
, LPWSTR lpDriverName
,
236 LPVOID lpContext
, HMONITOR hm
)
238 DirectDrawEnumerateProcData
*pEPD
= (DirectDrawEnumerateProcData
*)lpContext
;
240 return ((LPDDENUMCALLBACKW
) pEPD
->lpCallback
)(
241 lpGUID
, lpDriverDescription
, lpDriverName
, pEPD
->lpContext
);
244 HRESULT WINAPI
DirectDrawEnumerateW(
245 LPDDENUMCALLBACKW lpCallback
, LPVOID lpContext
)
247 DirectDrawEnumerateProcData epd
;
248 epd
.lpCallback
= (LPVOID
) lpCallback
;
249 epd
.lpContext
= lpContext
;
251 return DirectDrawEnumerateExW(DirectDrawEnumerateProcW
, (LPVOID
) &epd
, 0);
254 /***********************************************************************
255 * DirectDrawCreate (DDRAW.@)
258 const ddraw_driver
* DDRAW_FindDriver(const GUID
* pGUID
)
260 static const GUID zeroGUID
; /* gets zero-inited */
262 TRACE("(%s)\n", pGUID
? debugstr_guid(pGUID
) : "(null)");
264 if (DDRAW_num_drivers
== 0) return NULL
;
266 if (pGUID
== (LPGUID
)DDCREATE_EMULATIONONLY
267 || pGUID
== (LPGUID
)DDCREATE_HARDWAREONLY
)
270 if (pGUID
== NULL
|| memcmp(pGUID
, &zeroGUID
, sizeof(GUID
)) == 0)
272 /* Use the default driver. */
273 return DDRAW_drivers
[DDRAW_default_driver
];
277 /* Look for a matching GUID. */
280 for (i
=0; i
< DDRAW_num_drivers
; i
++)
282 if (IsEqualGUID(pGUID
,
283 &DDRAW_drivers
[i
]->info
->guidDeviceIdentifier
))
287 if (i
< DDRAW_num_drivers
)
289 return DDRAW_drivers
[i
];
293 ERR("(%s): did not recognize requested GUID.\n",debugstr_guid(pGUID
));
299 static HRESULT
DDRAW_Create(
300 LPGUID lpGUID
, LPVOID
*lplpDD
, LPUNKNOWN pUnkOuter
, REFIID iid
, BOOL ex
302 const ddraw_driver
* driver
;
306 TRACE("(%s,%p,%p,%d)\n", debugstr_guid(lpGUID
), lplpDD
, pUnkOuter
, ex
);
308 if (DDRAW_num_drivers
== 0)
310 WARN("no DirectDraw drivers registered\n");
311 return DDERR_INVALIDDIRECTDRAWGUID
;
314 if (lpGUID
== (LPGUID
)DDCREATE_EMULATIONONLY
315 || lpGUID
== (LPGUID
)DDCREATE_HARDWAREONLY
)
318 if (pUnkOuter
!= NULL
)
319 return DDERR_INVALIDPARAMS
; /* CLASS_E_NOAGGREGATION? */
321 driver
= DDRAW_FindDriver(lpGUID
);
322 if (driver
== NULL
) return DDERR_INVALIDDIRECTDRAWGUID
;
324 hr
= driver
->create(lpGUID
, &pDD
, pUnkOuter
, ex
);
325 if (FAILED(hr
)) return hr
;
327 hr
= IDirectDraw7_QueryInterface(pDD
, iid
, lplpDD
);
328 IDirectDraw7_Release(pDD
);
332 /***********************************************************************
333 * DirectDrawCreate (DDRAW.@)
335 * Only creates legacy IDirectDraw interfaces.
336 * Cannot create IDirectDraw7 interfaces.
339 HRESULT WINAPI
DirectDrawCreate(
340 LPGUID lpGUID
, LPDIRECTDRAW
* lplpDD
, LPUNKNOWN pUnkOuter
342 TRACE("(%s,%p,%p)\n", debugstr_guid(lpGUID
), lplpDD
, pUnkOuter
);
343 return DDRAW_Create(lpGUID
, (LPVOID
*) lplpDD
, pUnkOuter
, &IID_IDirectDraw
, FALSE
);
346 /***********************************************************************
347 * DirectDrawCreateEx (DDRAW.@)
349 * Only creates new IDirectDraw7 interfaces.
350 * Supposed to fail if legacy interfaces are requested.
353 HRESULT WINAPI
DirectDrawCreateEx(
354 LPGUID lpGUID
, LPVOID
* lplpDD
, REFIID iid
, LPUNKNOWN pUnkOuter
356 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(lpGUID
), lplpDD
, debugstr_guid(iid
), pUnkOuter
);
358 if (!IsEqualGUID(iid
, &IID_IDirectDraw7
))
359 return DDERR_INVALIDPARAMS
;
361 return DDRAW_Create(lpGUID
, lplpDD
, pUnkOuter
, iid
, TRUE
);
364 extern HRESULT
Uninit_DirectDraw_Create(const GUID
*, LPDIRECTDRAW7
*,
367 /* This is for the class factory. */
368 static HRESULT
DDRAW_CreateDirectDraw(IUnknown
* pUnkOuter
, REFIID iid
,
375 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppObj
);
377 /* This is a mighty hack :-) */
378 if (IsEqualGUID(iid
, &IID_IDirectDraw7
))
383 hr
= Uninit_DirectDraw_Create(NULL
, &pDD
, pUnkOuter
, ex
);
384 if (FAILED(hr
)) return hr
;
386 hr
= IDirectDraw7_QueryInterface(pDD
, iid
, ppObj
);
387 IDirectDraw_Release(pDD
);
391 /******************************************************************************
392 * DirectDraw ClassFactory
395 ICOM_VFIELD_MULTI(IClassFactory
);
398 HRESULT (*pfnCreateInstance
)(IUnknown
*pUnkOuter
, REFIID iid
,
402 struct object_creation_info
405 HRESULT (*pfnCreateInstance
)(IUnknown
*pUnkOuter
, REFIID riid
,
409 /* There should be more, but these are the only ones listed in the header
411 extern HRESULT
DDRAW_CreateDirectDrawClipper(IUnknown
*pUnkOuter
, REFIID riid
,
414 static const struct object_creation_info object_creation
[] =
416 { &CLSID_DirectDraw
, DDRAW_CreateDirectDraw
},
417 { &CLSID_DirectDraw7
, DDRAW_CreateDirectDraw
},
418 { &CLSID_DirectDrawClipper
, DDRAW_CreateDirectDrawClipper
}
421 static HRESULT WINAPI
422 DDCF_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
,LPVOID
*ppobj
)
424 ICOM_THIS(IClassFactoryImpl
,iface
);
426 TRACE("(%p)->(%s,%p)\n", This
, debugstr_guid(riid
), ppobj
);
428 if (IsEqualGUID(riid
, &IID_IUnknown
)
429 || IsEqualGUID(riid
, &IID_IClassFactory
))
431 IClassFactory_AddRef(iface
);
436 WARN("(%p)->(%s,%p),not found\n",This
,debugstr_guid(riid
),ppobj
);
437 return E_NOINTERFACE
;
440 static ULONG WINAPI
DDCF_AddRef(LPCLASSFACTORY iface
) {
441 ICOM_THIS(IClassFactoryImpl
,iface
);
443 TRACE("(%p)->() incrementing from %ld.\n", This
, This
->ref
);
445 return ++(This
->ref
);
448 static ULONG WINAPI
DDCF_Release(LPCLASSFACTORY iface
) {
449 ICOM_THIS(IClassFactoryImpl
,iface
);
451 TRACE("(%p)->() decrementing from %ld.\n", This
, This
->ref
);
453 if (--This
->ref
== 0)
454 HeapFree(GetProcessHeap(), 0, This
);
460 static HRESULT WINAPI
DDCF_CreateInstance(
461 LPCLASSFACTORY iface
,LPUNKNOWN pOuter
,REFIID riid
,LPVOID
*ppobj
463 ICOM_THIS(IClassFactoryImpl
,iface
);
465 TRACE("(%p)->(%p,%s,%p)\n",This
,pOuter
,debugstr_guid(riid
),ppobj
);
467 return This
->pfnCreateInstance(pOuter
, riid
, ppobj
);
470 static HRESULT WINAPI
DDCF_LockServer(LPCLASSFACTORY iface
,BOOL dolock
) {
471 ICOM_THIS(IClassFactoryImpl
,iface
);
472 FIXME("(%p)->(%d),stub!\n",This
,dolock
);
476 static ICOM_VTABLE(IClassFactory
) DDCF_Vtbl
=
478 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
486 /*******************************************************************************
487 * DllGetClassObject [DDRAW.@]
488 * Retrieves class object from a DLL object
491 * Docs say returns STDAPI
494 * rclsid [I] CLSID for the class object
495 * riid [I] Reference to identifier of interface for class object
496 * ppv [O] Address of variable to receive interface pointer for riid
500 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
503 DWORD WINAPI
DDRAW_DllGetClassObject(REFCLSID rclsid
,REFIID riid
,LPVOID
*ppv
)
506 IClassFactoryImpl
*factory
;
508 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
510 if ( !IsEqualGUID( &IID_IClassFactory
, riid
)
511 && ! IsEqualGUID( &IID_IUnknown
, riid
) )
512 return E_NOINTERFACE
;
514 for (i
=0; i
< sizeof(object_creation
)/sizeof(object_creation
[0]); i
++)
516 if (IsEqualGUID(object_creation
[i
].clsid
, rclsid
))
520 if (i
== sizeof(object_creation
)/sizeof(object_creation
[0]))
522 FIXME("%s: no class found.\n", debugstr_guid(rclsid
));
523 return CLASS_E_CLASSNOTAVAILABLE
;
526 factory
= HeapAlloc(GetProcessHeap(), 0, sizeof(*factory
));
527 if (factory
== NULL
) return E_OUTOFMEMORY
;
529 ICOM_INIT_INTERFACE(factory
, IClassFactory
, DDCF_Vtbl
);
532 factory
->pfnCreateInstance
= object_creation
[i
].pfnCreateInstance
;
534 *ppv
= ICOM_INTERFACE(factory
, IClassFactory
);
539 /*******************************************************************************
540 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
546 DWORD WINAPI
DDRAW_DllCanUnloadNow(void) {
547 FIXME("(void): stub\n");
551 /******************************************************************************
555 /* Choose which driver is considered the primary display driver. It will
556 * be created when we get a NULL guid for the DirectDrawCreate(Ex). */
557 static int DDRAW_ChooseDefaultDriver(void)
563 assert(DDRAW_num_drivers
> 0);
565 /* This algorithm is really stupid. */
566 for (i
=0; i
< DDRAW_num_drivers
; i
++)
568 if (DDRAW_drivers
[i
]->preference
> best_score
)
570 best_score
= DDRAW_drivers
[i
]->preference
;
575 assert(best_score
> 0);
580 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpv
)
582 /* If we were sufficiently cool, DDraw drivers would just be COM
583 * objects, registered with a particular component category. */
585 DDRAW_HAL_Init(hInstDLL
, fdwReason
, lpv
);
586 DDRAW_User_Init(hInstDLL
, fdwReason
, lpv
);
588 if (fdwReason
== DLL_PROCESS_ATTACH
)
592 DisableThreadLibraryCalls(hInstDLL
);
594 mod
= GetModuleHandleA( "x11drv.dll" );
597 wine_tsx11_lock_ptr
= (void *)GetProcAddress( mod
, "wine_tsx11_lock" );
598 wine_tsx11_unlock_ptr
= (void *)GetProcAddress( mod
, "wine_tsx11_unlock" );
601 opengl_initialized
= DDRAW_bind_to_opengl();
602 #endif /* HAVE_OPENGL */
605 if (DDRAW_num_drivers
> 0)
606 DDRAW_default_driver
= DDRAW_ChooseDefaultDriver();
611 /* Register a direct draw driver. This should be called from your init
612 * function. (That's why there is no locking: your init func is called from
613 * our DllInit, which is serialised.) */
614 void DDRAW_register_driver(const ddraw_driver
*driver
)
618 for (i
= 0; i
< DDRAW_num_drivers
; i
++)
620 if (DDRAW_drivers
[i
] == driver
)
622 ERR("Driver reregistering %p\n", driver
);
627 if (DDRAW_num_drivers
== sizeof(DDRAW_drivers
)/sizeof(DDRAW_drivers
[0]))
629 ERR("too many DDRAW drivers\n");
633 DDRAW_drivers
[DDRAW_num_drivers
++] = driver
;
636 /* This totally doesn't belong here. */
637 LONG
DDRAW_width_bpp_to_pitch(DWORD width
, DWORD bpp
)
641 assert(bpp
!= 0); /* keeps happening... */
643 if (bpp
== 15) bpp
= 16;
644 pitch
= width
* (bpp
/ 8);
645 return pitch
+ (8 - (pitch
% 8)) % 8;