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