wined3d: Disable the X11 / GL lock.
[wine] / dlls / wined3d / wined3d_main.c
1 /*
2  * Direct3D wine internal interface main
3  *
4  * Copyright 2002-2003 The wine-d3d team
5  * Copyright 2002-2003 Raphael Junqueira
6  * Copyright 2004      Jason Edmeades
7  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8  * Copyright 2009 Henri Verbeet for CodeWeavers
9  *
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.
14  *
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.
19  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include "initguid.h"
29 #include "wined3d_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32 WINE_DECLARE_DEBUG_CHANNEL(winediag);
33
34 struct wined3d_wndproc
35 {
36     HWND window;
37     BOOL unicode;
38     WNDPROC proc;
39     struct wined3d_device *device;
40 };
41
42 struct wined3d_wndproc_table
43 {
44     struct wined3d_wndproc *entries;
45     unsigned int count;
46     unsigned int size;
47 };
48
49 static struct wined3d_wndproc_table wndproc_table;
50
51 int num_lock = 0;
52 void (CDECL *wine_tsx11_lock_ptr)(void) = NULL;
53 void (CDECL *wine_tsx11_unlock_ptr)(void) = NULL;
54
55 static CRITICAL_SECTION wined3d_cs;
56 static CRITICAL_SECTION_DEBUG wined3d_cs_debug =
57 {
58     0, 0, &wined3d_cs,
59     {&wined3d_cs_debug.ProcessLocksList,
60     &wined3d_cs_debug.ProcessLocksList},
61     0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_cs")}
62 };
63 static CRITICAL_SECTION wined3d_cs = {&wined3d_cs_debug, -1, 0, 0, 0, 0};
64
65 static CRITICAL_SECTION wined3d_wndproc_cs;
66 static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug =
67 {
68     0, 0, &wined3d_wndproc_cs,
69     {&wined3d_wndproc_cs_debug.ProcessLocksList,
70     &wined3d_wndproc_cs_debug.ProcessLocksList},
71     0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_wndproc_cs")}
72 };
73 static CRITICAL_SECTION wined3d_wndproc_cs = {&wined3d_wndproc_cs_debug, -1, 0, 0, 0, 0};
74
75 /* When updating default value here, make sure to update winecfg as well,
76  * where appropriate. */
77 struct wined3d_settings wined3d_settings =
78 {
79     VS_HW,          /* Hardware by default */
80     PS_HW,          /* Hardware by default */
81     TRUE,           /* Use of GLSL enabled by default */
82     ORM_FBO,        /* Use FBOs to do offscreen rendering */
83     RTL_READTEX,    /* Default render target locking method */
84     PCI_VENDOR_NONE,/* PCI Vendor ID */
85     PCI_DEVICE_NONE,/* PCI Device ID */
86     0,              /* The default of memory is set in init_driver_info */
87     NULL,           /* No wine logo by default */
88     TRUE,           /* Multisampling enabled by default. */
89     FALSE,          /* No strict draw ordering. */
90     FALSE,          /* Try to render onscreen by default. */
91 };
92
93 /* Do not call while under the GL lock. */
94 struct wined3d * CDECL wined3d_create(UINT version, DWORD flags)
95 {
96     struct wined3d *object;
97     HRESULT hr;
98
99     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(struct wined3d, adapters[1]));
100     if (!object)
101     {
102         ERR("Failed to allocate wined3d object memory.\n");
103         return NULL;
104     }
105
106     hr = wined3d_init(object, version, flags);
107     if (FAILED(hr))
108     {
109         WARN("Failed to initialize wined3d object, hr %#x.\n", hr);
110         HeapFree(GetProcessHeap(), 0, object);
111         return NULL;
112     }
113
114     TRACE("Created wined3d object %p for d3d%d support.\n", object, version);
115
116     return object;
117 }
118
119 static DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name, char *buffer, DWORD size)
120 {
121     if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
122     if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
123     return ERROR_FILE_NOT_FOUND;
124 }
125
126 static DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char *name, DWORD *data)
127 {
128     DWORD type;
129     DWORD size = sizeof(DWORD);
130     if (appkey && !RegQueryValueExA(appkey, name, 0, &type, (BYTE *)data, &size) && (type == REG_DWORD)) return 0;
131     if (defkey && !RegQueryValueExA(defkey, name, 0, &type, (BYTE *)data, &size) && (type == REG_DWORD)) return 0;
132     return ERROR_FILE_NOT_FOUND;
133 }
134
135 static void CDECL wined3d_do_nothing(void)
136 {
137 }
138
139 static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
140 {
141     DWORD wined3d_context_tls_idx;
142     HMODULE mod;
143     char buffer[MAX_PATH+10];
144     DWORD size = sizeof(buffer);
145     HKEY hkey = 0;
146     HKEY appkey = 0;
147     DWORD len, tmpvalue;
148     WNDCLASSA wc;
149
150     wined3d_context_tls_idx = TlsAlloc();
151     if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
152     {
153         DWORD err = GetLastError();
154         ERR("Failed to allocate context TLS index, err %#x.\n", err);
155         return FALSE;
156     }
157     context_set_tls_idx(wined3d_context_tls_idx);
158
159     /* We need our own window class for a fake window which we use to retrieve GL capabilities */
160     /* We might need CS_OWNDC in the future if we notice strange things on Windows.
161      * Various articles/posts about OpenGL problems on Windows recommend this. */
162     wc.style                = CS_HREDRAW | CS_VREDRAW;
163     wc.lpfnWndProc          = DefWindowProcA;
164     wc.cbClsExtra           = 0;
165     wc.cbWndExtra           = 0;
166     wc.hInstance            = hInstDLL;
167     wc.hIcon                = LoadIconA(NULL, (LPCSTR)IDI_WINLOGO);
168     wc.hCursor              = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
169     wc.hbrBackground        = NULL;
170     wc.lpszMenuName         = NULL;
171     wc.lpszClassName        = WINED3D_OPENGL_WINDOW_CLASS_NAME;
172
173     if (!RegisterClassA(&wc))
174     {
175         ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
176         if (!TlsFree(wined3d_context_tls_idx))
177         {
178             DWORD err = GetLastError();
179             ERR("Failed to free context TLS index, err %#x.\n", err);
180         }
181         return FALSE;
182     }
183
184     DisableThreadLibraryCalls(hInstDLL);
185
186     mod = GetModuleHandleA( "winex11.drv" );
187     if (mod)
188     {
189         wine_tsx11_lock_ptr   = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
190         wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
191     }
192     else /* We are most likely on Windows */
193     {
194         wine_tsx11_lock_ptr   = wined3d_do_nothing;
195         wine_tsx11_unlock_ptr = wined3d_do_nothing;
196     }
197     /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
198     if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
199
200     len = GetModuleFileNameA( 0, buffer, MAX_PATH );
201     if (len && len < MAX_PATH)
202     {
203         HKEY tmpkey;
204         /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
205         if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
206         {
207             char *p, *appname = buffer;
208             if ((p = strrchr( appname, '/' ))) appname = p + 1;
209             if ((p = strrchr( appname, '\\' ))) appname = p + 1;
210             strcat( appname, "\\Direct3D" );
211             TRACE("appname = [%s]\n", appname);
212             if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
213             RegCloseKey( tmpkey );
214         }
215     }
216
217     if (hkey || appkey)
218     {
219         if ( !get_config_key( hkey, appkey, "VertexShaderMode", buffer, size) )
220         {
221             if (!strcmp(buffer,"none"))
222             {
223                 TRACE("Disable vertex shaders\n");
224                 wined3d_settings.vs_mode = VS_NONE;
225             }
226         }
227         if ( !get_config_key( hkey, appkey, "PixelShaderMode", buffer, size) )
228         {
229             if (!strcmp(buffer,"enabled"))
230             {
231                 TRACE("Allow pixel shaders\n");
232                 wined3d_settings.ps_mode = PS_HW;
233             }
234             if (!strcmp(buffer,"disabled"))
235             {
236                 TRACE("Disable pixel shaders\n");
237                 wined3d_settings.ps_mode = PS_NONE;
238             }
239         }
240         if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
241         {
242             if (!strcmp(buffer,"disabled"))
243             {
244                 ERR_(winediag)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
245                 TRACE("Use of GL Shading Language disabled\n");
246                 wined3d_settings.glslRequested = FALSE;
247             }
248         }
249         if ( !get_config_key( hkey, appkey, "OffscreenRenderingMode", buffer, size) )
250         {
251             if (!strcmp(buffer,"backbuffer"))
252             {
253                 TRACE("Using the backbuffer for offscreen rendering\n");
254                 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
255             }
256             else if (!strcmp(buffer,"fbo"))
257             {
258                 TRACE("Using FBOs for offscreen rendering\n");
259                 wined3d_settings.offscreen_rendering_mode = ORM_FBO;
260             }
261         }
262         if ( !get_config_key( hkey, appkey, "RenderTargetLockMode", buffer, size) )
263         {
264             if (!strcmp(buffer,"readdraw"))
265             {
266                 TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
267                 wined3d_settings.rendertargetlock_mode = RTL_READDRAW;
268             }
269             else if (!strcmp(buffer,"readtex"))
270             {
271                 TRACE("Using glReadPixels for render target reading and textures for writing\n");
272                 wined3d_settings.rendertargetlock_mode = RTL_READTEX;
273             }
274         }
275         if ( !get_config_key_dword( hkey, appkey, "VideoPciDeviceID", &tmpvalue) )
276         {
277             int pci_device_id = tmpvalue;
278
279             /* A pci device id is 16-bit */
280             if(pci_device_id > 0xffff)
281             {
282                 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
283             }
284             else
285             {
286                 TRACE("Using PCI Device ID %04x\n", pci_device_id);
287                 wined3d_settings.pci_device_id = pci_device_id;
288             }
289         }
290         if ( !get_config_key_dword( hkey, appkey, "VideoPciVendorID", &tmpvalue) )
291         {
292             int pci_vendor_id = tmpvalue;
293
294             /* A pci device id is 16-bit */
295             if(pci_vendor_id > 0xffff)
296             {
297                 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
298             }
299             else
300             {
301                 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
302                 wined3d_settings.pci_vendor_id = pci_vendor_id;
303             }
304         }
305         if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
306         {
307             int TmpVideoMemorySize = atoi(buffer);
308             if(TmpVideoMemorySize > 0)
309             {
310                 wined3d_settings.emulated_textureram = TmpVideoMemorySize *1024*1024;
311                 TRACE("Use %iMB = %d byte for emulated_textureram\n",
312                         TmpVideoMemorySize,
313                         wined3d_settings.emulated_textureram);
314             }
315             else
316                 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
317         }
318         if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
319         {
320             size_t len = strlen(buffer) + 1;
321
322             wined3d_settings.logo = HeapAlloc(GetProcessHeap(), 0, len);
323             if (!wined3d_settings.logo) ERR("Failed to allocate logo path memory.\n");
324             else memcpy(wined3d_settings.logo, buffer, len);
325         }
326         if ( !get_config_key( hkey, appkey, "Multisampling", buffer, size) )
327         {
328             if (!strcmp(buffer, "disabled"))
329             {
330                 TRACE("Multisampling disabled.\n");
331                 wined3d_settings.allow_multisampling = FALSE;
332             }
333         }
334         if (!get_config_key(hkey, appkey, "StrictDrawOrdering", buffer, size)
335                 && !strcmp(buffer,"enabled"))
336         {
337             TRACE("Enforcing strict draw ordering.\n");
338             wined3d_settings.strict_draw_ordering = TRUE;
339         }
340         if (!get_config_key(hkey, appkey, "AlwaysOffscreen", buffer, size)
341                 && !strcmp(buffer,"enabled"))
342         {
343             TRACE("Always rendering backbuffers offscreen.\n");
344             wined3d_settings.always_offscreen = TRUE;
345         }
346     }
347     if (wined3d_settings.vs_mode == VS_HW)
348         TRACE("Allow HW vertex shaders\n");
349     if (wined3d_settings.ps_mode == PS_NONE)
350         TRACE("Disable pixel shaders\n");
351     if (wined3d_settings.glslRequested)
352         TRACE("If supported by your system, GL Shading Language will be used\n");
353
354     if (appkey) RegCloseKey( appkey );
355     if (hkey) RegCloseKey( hkey );
356
357     return TRUE;
358 }
359
360 static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
361 {
362     DWORD wined3d_context_tls_idx = context_get_tls_idx();
363     unsigned int i;
364
365     if (!TlsFree(wined3d_context_tls_idx))
366     {
367         DWORD err = GetLastError();
368         ERR("Failed to free context TLS index, err %#x.\n", err);
369     }
370
371     for (i = 0; i < wndproc_table.count; ++i)
372     {
373         /* Trying to unregister these would be futile. These entries can only
374          * exist if either we skipped them in wined3d_unregister_window() due
375          * to the application replacing the wndproc after the entry was
376          * registered, or if the application still has an active wined3d
377          * device. In the latter case the application has bigger problems than
378          * these entries. */
379         WARN("Leftover wndproc table entry %p.\n", &wndproc_table.entries[i]);
380     }
381     HeapFree(GetProcessHeap(), 0, wndproc_table.entries);
382
383     HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
384     UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
385
386     DeleteCriticalSection(&wined3d_wndproc_cs);
387     DeleteCriticalSection(&wined3d_cs);
388     return TRUE;
389 }
390
391 void WINAPI wined3d_mutex_lock(void)
392 {
393     EnterCriticalSection(&wined3d_cs);
394 }
395
396 void WINAPI wined3d_mutex_unlock(void)
397 {
398     LeaveCriticalSection(&wined3d_cs);
399 }
400
401 static void wined3d_wndproc_mutex_lock(void)
402 {
403     EnterCriticalSection(&wined3d_wndproc_cs);
404 }
405
406 static void wined3d_wndproc_mutex_unlock(void)
407 {
408     LeaveCriticalSection(&wined3d_wndproc_cs);
409 }
410
411 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window)
412 {
413     unsigned int i;
414
415     for (i = 0; i < wndproc_table.count; ++i)
416     {
417         if (wndproc_table.entries[i].window == window)
418         {
419             return &wndproc_table.entries[i];
420         }
421     }
422
423     return NULL;
424 }
425
426 static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
427 {
428     struct wined3d_wndproc *entry;
429     struct wined3d_device *device;
430     BOOL unicode;
431     WNDPROC proc;
432
433     wined3d_wndproc_mutex_lock();
434     entry = wined3d_find_wndproc(window);
435
436     if (!entry)
437     {
438         wined3d_wndproc_mutex_unlock();
439         ERR("Window %p is not registered with wined3d.\n", window);
440         return DefWindowProcW(window, message, wparam, lparam);
441     }
442
443     device = entry->device;
444     unicode = entry->unicode;
445     proc = entry->proc;
446     wined3d_wndproc_mutex_unlock();
447
448     if (device)
449         return device_process_message(device, window, unicode, message, wparam, lparam, proc);
450     if (unicode)
451         return CallWindowProcW(proc, window, message, wparam, lparam);
452     return CallWindowProcA(proc, window, message, wparam, lparam);
453 }
454
455 BOOL wined3d_register_window(HWND window, struct wined3d_device *device)
456 {
457     struct wined3d_wndproc *entry;
458
459     wined3d_wndproc_mutex_lock();
460
461     if (wined3d_find_wndproc(window))
462     {
463         wined3d_wndproc_mutex_unlock();
464         WARN("Window %p is already registered with wined3d.\n", window);
465         return TRUE;
466     }
467
468     if (wndproc_table.size == wndproc_table.count)
469     {
470         unsigned int new_size = max(1, wndproc_table.size * 2);
471         struct wined3d_wndproc *new_entries;
472
473         if (!wndproc_table.entries) new_entries = HeapAlloc(GetProcessHeap(), 0, new_size * sizeof(*new_entries));
474         else new_entries = HeapReAlloc(GetProcessHeap(), 0, wndproc_table.entries, new_size * sizeof(*new_entries));
475
476         if (!new_entries)
477         {
478             wined3d_wndproc_mutex_unlock();
479             ERR("Failed to grow table.\n");
480             return FALSE;
481         }
482
483         wndproc_table.entries = new_entries;
484         wndproc_table.size = new_size;
485     }
486
487     entry = &wndproc_table.entries[wndproc_table.count++];
488     entry->window = window;
489     entry->unicode = IsWindowUnicode(window);
490     /* Set a window proc that matches the window. Some applications (e.g. NoX)
491      * replace the window proc after we've set ours, and expect to be able to
492      * call the previous one (ours) directly, without using CallWindowProc(). */
493     if (entry->unicode)
494         entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
495     else
496         entry->proc = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
497     entry->device = device;
498
499     wined3d_wndproc_mutex_unlock();
500
501     return TRUE;
502 }
503
504 void wined3d_unregister_window(HWND window)
505 {
506     struct wined3d_wndproc *entry, *last;
507     LONG_PTR proc;
508
509     wined3d_wndproc_mutex_lock();
510
511     if (!(entry = wined3d_find_wndproc(window)))
512     {
513         wined3d_wndproc_mutex_unlock();
514         ERR("Window %p is not registered with wined3d.\n", window);
515         return;
516     }
517
518     if (entry->unicode)
519     {
520         proc = GetWindowLongPtrW(window, GWLP_WNDPROC);
521         if (proc != (LONG_PTR)wined3d_wndproc)
522         {
523             entry->device = NULL;
524             wined3d_wndproc_mutex_unlock();
525             WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
526                     window, proc, wined3d_wndproc);
527             return;
528         }
529
530         SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
531     }
532     else
533     {
534         proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
535         if (proc != (LONG_PTR)wined3d_wndproc)
536         {
537             entry->device = NULL;
538             wined3d_wndproc_mutex_unlock();
539             WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
540                     window, proc, wined3d_wndproc);
541             return;
542         }
543
544         SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
545     }
546
547     last = &wndproc_table.entries[--wndproc_table.count];
548     if (entry != last) *entry = *last;
549
550     wined3d_wndproc_mutex_unlock();
551 }
552
553 /* At process attach */
554 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
555 {
556     TRACE("WineD3D DLLMain Reason=%u\n", fdwReason);
557
558     switch (fdwReason)
559     {
560         case DLL_PROCESS_ATTACH:
561             return wined3d_dll_init(hInstDLL);
562
563         case DLL_PROCESS_DETACH:
564             return wined3d_dll_destroy(hInstDLL);
565
566         case DLL_THREAD_DETACH:
567         {
568             if (!context_set_current(NULL))
569             {
570                 ERR("Failed to clear current context.\n");
571             }
572             return TRUE;
573         }
574
575         default:
576             return TRUE;
577     }
578 }