1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "cr_spu.h"
|
---|
8 | #include "cr_net.h"
|
---|
9 | #include "cr_error.h"
|
---|
10 | #include "cr_mem.h"
|
---|
11 | #include "cr_string.h"
|
---|
12 | #include "cr_net.h"
|
---|
13 | #include "cr_environment.h"
|
---|
14 | #include "cr_process.h"
|
---|
15 | #include "cr_rand.h"
|
---|
16 | #include "cr_netserver.h"
|
---|
17 | #include "stub.h"
|
---|
18 | #include <stdlib.h>
|
---|
19 | #include <string.h>
|
---|
20 | #include <signal.h>
|
---|
21 | #include <iprt/initterm.h>
|
---|
22 | #include <iprt/thread.h>
|
---|
23 | #include <iprt/err.h>
|
---|
24 | #include <iprt/asm.h>
|
---|
25 | #ifndef WINDOWS
|
---|
26 | # include <sys/types.h>
|
---|
27 | # include <unistd.h>
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | #ifdef VBOX_WITH_WDDM
|
---|
31 | #include <d3d9types.h>
|
---|
32 | #include <D3dumddi.h>
|
---|
33 | #include "../../WINNT/Graphics/Video/common/wddm/VBoxMPIf.h"
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * If you change this, see the comments in tilesortspu_context.c
|
---|
38 | */
|
---|
39 | #define MAGIC_CONTEXT_BASE 500
|
---|
40 |
|
---|
41 | #define CONFIG_LOOKUP_FILE ".crconfigs"
|
---|
42 |
|
---|
43 | #ifdef WINDOWS
|
---|
44 | #define PYTHON_EXE "python.exe"
|
---|
45 | #else
|
---|
46 | #define PYTHON_EXE "python"
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | static bool stub_initialized = 0;
|
---|
50 | #ifdef WINDOWS
|
---|
51 | static CRmutex stub_init_mutex;
|
---|
52 | #define STUB_INIT_LOCK() do { crLockMutex(&stub_init_mutex); } while (0)
|
---|
53 | #define STUB_INIT_UNLOCK() do { crUnlockMutex(&stub_init_mutex); } while (0)
|
---|
54 | #else
|
---|
55 | #define STUB_INIT_LOCK() do { } while (0)
|
---|
56 | #define STUB_INIT_UNLOCK() do { } while (0)
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | /* NOTE: 'SPUDispatchTable glim' is declared in NULLfuncs.py now */
|
---|
60 | /* NOTE: 'SPUDispatchTable stubThreadsafeDispatch' is declared in tsfuncs.c */
|
---|
61 | Stub stub;
|
---|
62 | #ifdef CHROMIUM_THREADSAFE
|
---|
63 | static bool g_stubIsCurrentContextTSDInited;
|
---|
64 | CRtsd g_stubCurrentContextTSD;
|
---|
65 | #endif
|
---|
66 |
|
---|
67 |
|
---|
68 | static void stubInitNativeDispatch( void )
|
---|
69 | {
|
---|
70 | #define MAX_FUNCS 1000
|
---|
71 | SPUNamedFunctionTable gl_funcs[MAX_FUNCS];
|
---|
72 | int numFuncs;
|
---|
73 |
|
---|
74 | numFuncs = crLoadOpenGL( &stub.wsInterface, gl_funcs );
|
---|
75 |
|
---|
76 | stub.haveNativeOpenGL = (numFuncs > 0);
|
---|
77 |
|
---|
78 | /* XXX call this after context binding */
|
---|
79 | numFuncs += crLoadOpenGLExtensions( &stub.wsInterface, gl_funcs + numFuncs );
|
---|
80 |
|
---|
81 | CRASSERT(numFuncs < MAX_FUNCS);
|
---|
82 |
|
---|
83 | crSPUInitDispatchTable( &stub.nativeDispatch );
|
---|
84 | crSPUInitDispatch( &stub.nativeDispatch, gl_funcs );
|
---|
85 | crSPUInitDispatchNops( &stub.nativeDispatch );
|
---|
86 | #undef MAX_FUNCS
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | /** Pointer to the SPU's real glClear and glViewport functions */
|
---|
91 | static ClearFunc_t origClear;
|
---|
92 | static ViewportFunc_t origViewport;
|
---|
93 | static SwapBuffersFunc_t origSwapBuffers;
|
---|
94 | static DrawBufferFunc_t origDrawBuffer;
|
---|
95 | static ScissorFunc_t origScissor;
|
---|
96 |
|
---|
97 | static void stubCheckWindowState(WindowInfo *window, GLboolean bFlushOnChange)
|
---|
98 | {
|
---|
99 | bool bForceUpdate = false;
|
---|
100 | bool bChanged = false;
|
---|
101 |
|
---|
102 | #ifdef WINDOWS
|
---|
103 | /* @todo install hook and track for WM_DISPLAYCHANGE */
|
---|
104 | {
|
---|
105 | DEVMODE devMode;
|
---|
106 |
|
---|
107 | devMode.dmSize = sizeof(DEVMODE);
|
---|
108 | EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode);
|
---|
109 |
|
---|
110 | if (devMode.dmPelsWidth!=window->dmPelsWidth || devMode.dmPelsHeight!=window->dmPelsHeight)
|
---|
111 | {
|
---|
112 | crDebug("Resolution changed(%d,%d), forcing window Pos/Size update", devMode.dmPelsWidth, devMode.dmPelsHeight);
|
---|
113 | window->dmPelsWidth = devMode.dmPelsWidth;
|
---|
114 | window->dmPelsHeight = devMode.dmPelsHeight;
|
---|
115 | bForceUpdate = true;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | #endif
|
---|
119 |
|
---|
120 | bChanged = stubUpdateWindowGeometry(window, bForceUpdate) || bForceUpdate;
|
---|
121 |
|
---|
122 | #if defined(GLX) || defined (WINDOWS)
|
---|
123 | if (stub.trackWindowVisibleRgn)
|
---|
124 | {
|
---|
125 | bChanged = stubUpdateWindowVisibileRegions(window) || bChanged;
|
---|
126 | }
|
---|
127 | #endif
|
---|
128 |
|
---|
129 | if (stub.trackWindowVisibility && window->type == CHROMIUM && window->drawable) {
|
---|
130 | const int mapped = stubIsWindowVisible(window);
|
---|
131 | if (mapped != window->mapped) {
|
---|
132 | crDebug("Dispatched: WindowShow(%i, %i)", window->spuWindow, mapped);
|
---|
133 | stub.spu->dispatch_table.WindowShow(window->spuWindow, mapped);
|
---|
134 | window->mapped = mapped;
|
---|
135 | bChanged = true;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (bFlushOnChange && bChanged)
|
---|
140 | {
|
---|
141 | stub.spu->dispatch_table.Flush();
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | static bool stubSystemWindowExist(WindowInfo *pWindow)
|
---|
146 | {
|
---|
147 | #ifdef WINDOWS
|
---|
148 | if (pWindow->hWnd!=WindowFromDC(pWindow->drawable))
|
---|
149 | {
|
---|
150 | return false;
|
---|
151 | }
|
---|
152 | #else
|
---|
153 | Window root;
|
---|
154 | int x, y;
|
---|
155 | unsigned int border, depth, w, h;
|
---|
156 | Display *dpy;
|
---|
157 |
|
---|
158 | dpy = stubGetWindowDisplay(pWindow);
|
---|
159 |
|
---|
160 | XLOCK(dpy);
|
---|
161 | if (!XGetGeometry(dpy, pWindow->drawable, &root, &x, &y, &w, &h, &border, &depth))
|
---|
162 | {
|
---|
163 | XUNLOCK(dpy);
|
---|
164 | return false;
|
---|
165 | }
|
---|
166 | XUNLOCK(dpy);
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | return true;
|
---|
170 | }
|
---|
171 |
|
---|
172 | static void stubCheckWindowsCB(unsigned long key, void *data1, void *data2)
|
---|
173 | {
|
---|
174 | WindowInfo *pWindow = (WindowInfo *) data1;
|
---|
175 | ContextInfo *pCtx = (ContextInfo *) data2;
|
---|
176 |
|
---|
177 | if (pWindow == pCtx->currentDrawable
|
---|
178 | || pWindow->type!=CHROMIUM
|
---|
179 | || pWindow->pOwner!=pCtx)
|
---|
180 | {
|
---|
181 | return;
|
---|
182 | }
|
---|
183 |
|
---|
184 | if (!stubSystemWindowExist(pWindow))
|
---|
185 | {
|
---|
186 | #ifdef WINDOWS
|
---|
187 | stubDestroyWindow(CR_CTX_CON(pCtx), (GLint)pWindow->hWnd);
|
---|
188 | #else
|
---|
189 | stubDestroyWindow(CR_CTX_CON(pCtx), (GLint)pWindow->drawable);
|
---|
190 | #endif
|
---|
191 | return;
|
---|
192 | }
|
---|
193 |
|
---|
194 | stubCheckWindowState(pWindow, GL_FALSE);
|
---|
195 | }
|
---|
196 |
|
---|
197 | static void stubCheckWindowsState(void)
|
---|
198 | {
|
---|
199 | ContextInfo *context = stubGetCurrentContext();
|
---|
200 |
|
---|
201 | CRASSERT(stub.trackWindowSize || stub.trackWindowPos);
|
---|
202 |
|
---|
203 | if (!context)
|
---|
204 | return;
|
---|
205 |
|
---|
206 | #if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
|
---|
207 | if (stub.bRunningUnderWDDM)
|
---|
208 | return;
|
---|
209 | #endif
|
---|
210 |
|
---|
211 | /* Try to keep a consistent locking order. */
|
---|
212 | crHashtableLock(stub.windowTable);
|
---|
213 | #if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
|
---|
214 | crLockMutex(&stub.mutex);
|
---|
215 | #endif
|
---|
216 |
|
---|
217 | stubCheckWindowState(context->currentDrawable, GL_TRUE);
|
---|
218 | crHashtableWalkUnlocked(stub.windowTable, stubCheckWindowsCB, context);
|
---|
219 |
|
---|
220 | #if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
|
---|
221 | crUnlockMutex(&stub.mutex);
|
---|
222 | #endif
|
---|
223 | crHashtableUnlock(stub.windowTable);
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Override the head SPU's glClear function.
|
---|
229 | * We're basically trapping this function so that we can poll the
|
---|
230 | * application window size at a regular interval.
|
---|
231 | */
|
---|
232 | static void SPU_APIENTRY trapClear(GLbitfield mask)
|
---|
233 | {
|
---|
234 | stubCheckWindowsState();
|
---|
235 | /* call the original SPU glClear function */
|
---|
236 | origClear(mask);
|
---|
237 | }
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * As above, but for glViewport. Most apps call glViewport before
|
---|
241 | * glClear when a window is resized.
|
---|
242 | */
|
---|
243 | static void SPU_APIENTRY trapViewport(GLint x, GLint y, GLsizei w, GLsizei h)
|
---|
244 | {
|
---|
245 | stubCheckWindowsState();
|
---|
246 | /* call the original SPU glViewport function */
|
---|
247 | origViewport(x, y, w, h);
|
---|
248 | }
|
---|
249 |
|
---|
250 | static void SPU_APIENTRY trapSwapBuffers(GLint window, GLint flags)
|
---|
251 | {
|
---|
252 | stubCheckWindowsState();
|
---|
253 | origSwapBuffers(window, flags);
|
---|
254 | }
|
---|
255 |
|
---|
256 | static void SPU_APIENTRY trapDrawBuffer(GLenum buf)
|
---|
257 | {
|
---|
258 | stubCheckWindowsState();
|
---|
259 | origDrawBuffer(buf);
|
---|
260 | }
|
---|
261 |
|
---|
262 | static void SPU_APIENTRY trapScissor(GLint x, GLint y, GLsizei w, GLsizei h)
|
---|
263 | {
|
---|
264 | int winX, winY;
|
---|
265 | unsigned int winW, winH;
|
---|
266 | WindowInfo *pWindow;
|
---|
267 | ContextInfo *context = stubGetCurrentContext();
|
---|
268 | pWindow = context->currentDrawable;
|
---|
269 | stubGetWindowGeometry(pWindow, &winX, &winY, &winW, &winH);
|
---|
270 | origScissor(0, 0, winW, winH);
|
---|
271 | }
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Use the GL function pointers in <spu> to initialize the static glim
|
---|
275 | * dispatch table.
|
---|
276 | */
|
---|
277 | static void stubInitSPUDispatch(SPU *spu)
|
---|
278 | {
|
---|
279 | crSPUInitDispatchTable( &stub.spuDispatch );
|
---|
280 | crSPUCopyDispatchTable( &stub.spuDispatch, &(spu->dispatch_table) );
|
---|
281 |
|
---|
282 | if (stub.trackWindowSize || stub.trackWindowPos || stub.trackWindowVisibleRgn) {
|
---|
283 | /* patch-in special glClear/Viewport function to track window sizing */
|
---|
284 | origClear = stub.spuDispatch.Clear;
|
---|
285 | origViewport = stub.spuDispatch.Viewport;
|
---|
286 | origSwapBuffers = stub.spuDispatch.SwapBuffers;
|
---|
287 | origDrawBuffer = stub.spuDispatch.DrawBuffer;
|
---|
288 | origScissor = stub.spuDispatch.Scissor;
|
---|
289 | stub.spuDispatch.Clear = trapClear;
|
---|
290 | stub.spuDispatch.Viewport = trapViewport;
|
---|
291 |
|
---|
292 | /*stub.spuDispatch.SwapBuffers = trapSwapBuffers;
|
---|
293 | stub.spuDispatch.DrawBuffer = trapDrawBuffer;*/
|
---|
294 | }
|
---|
295 |
|
---|
296 | crSPUCopyDispatchTable( &glim, &stub.spuDispatch );
|
---|
297 | }
|
---|
298 |
|
---|
299 | // Callback function, used to destroy all created contexts
|
---|
300 | static void hsWalkStubDestroyContexts(unsigned long key, void *data1, void *data2)
|
---|
301 | {
|
---|
302 | stubDestroyContext(key);
|
---|
303 | }
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * This is called when we exit.
|
---|
307 | * We call all the SPU's cleanup functions.
|
---|
308 | */
|
---|
309 | static void stubSPUTearDownLocked(void)
|
---|
310 | {
|
---|
311 | crDebug("stubSPUTearDownLocked");
|
---|
312 |
|
---|
313 | #ifdef WINDOWS
|
---|
314 | # ifndef CR_NEWWINTRACK
|
---|
315 | stubUninstallWindowMessageHook();
|
---|
316 | # endif
|
---|
317 | #endif
|
---|
318 |
|
---|
319 | #ifdef CR_NEWWINTRACK
|
---|
320 | ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
|
---|
321 | #endif
|
---|
322 |
|
---|
323 | //delete all created contexts
|
---|
324 | stubMakeCurrent( NULL, NULL);
|
---|
325 |
|
---|
326 | /* the lock order is windowTable->contextTable (see wglMakeCurrent_prox, glXMakeCurrent)
|
---|
327 | * this is why we need to take a windowTable lock since we will later do stub.windowTable access & locking */
|
---|
328 | crHashtableLock(stub.windowTable);
|
---|
329 | crHashtableWalk(stub.contextTable, hsWalkStubDestroyContexts, NULL);
|
---|
330 | crHashtableUnlock(stub.windowTable);
|
---|
331 |
|
---|
332 | /* shutdown, now trap any calls to a NULL dispatcher */
|
---|
333 | crSPUCopyDispatchTable(&glim, &stubNULLDispatch);
|
---|
334 |
|
---|
335 | crSPUUnloadChain(stub.spu);
|
---|
336 | stub.spu = NULL;
|
---|
337 |
|
---|
338 | #ifndef Linux
|
---|
339 | crUnloadOpenGL();
|
---|
340 | #endif
|
---|
341 |
|
---|
342 | #ifndef WINDOWS
|
---|
343 | crNetTearDown();
|
---|
344 | #endif
|
---|
345 |
|
---|
346 | #ifdef GLX
|
---|
347 | if (stub.xshmSI.shmid>=0)
|
---|
348 | {
|
---|
349 | shmctl(stub.xshmSI.shmid, IPC_RMID, 0);
|
---|
350 | shmdt(stub.xshmSI.shmaddr);
|
---|
351 | }
|
---|
352 | crFreeHashtable(stub.pGLXPixmapsHash, crFree);
|
---|
353 | #endif
|
---|
354 |
|
---|
355 | crFreeHashtable(stub.windowTable, crFree);
|
---|
356 | crFreeHashtable(stub.contextTable, NULL);
|
---|
357 |
|
---|
358 | crMemset(&stub, 0, sizeof(stub));
|
---|
359 |
|
---|
360 | }
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * This is called when we exit.
|
---|
364 | * We call all the SPU's cleanup functions.
|
---|
365 | */
|
---|
366 | static void stubSPUTearDown(void)
|
---|
367 | {
|
---|
368 | STUB_INIT_LOCK();
|
---|
369 | if (stub_initialized)
|
---|
370 | {
|
---|
371 | stubSPUTearDownLocked();
|
---|
372 | stub_initialized = 0;
|
---|
373 | }
|
---|
374 | STUB_INIT_UNLOCK();
|
---|
375 | }
|
---|
376 |
|
---|
377 | static void stubSPUSafeTearDown(void)
|
---|
378 | {
|
---|
379 | #ifdef CHROMIUM_THREADSAFE
|
---|
380 | CRmutex *mutex;
|
---|
381 | #endif
|
---|
382 |
|
---|
383 | if (!stub_initialized) return;
|
---|
384 | stub_initialized = 0;
|
---|
385 |
|
---|
386 | #ifdef CHROMIUM_THREADSAFE
|
---|
387 | mutex = &stub.mutex;
|
---|
388 | crLockMutex(mutex);
|
---|
389 | #endif
|
---|
390 | crDebug("stubSPUSafeTearDown");
|
---|
391 |
|
---|
392 | #ifdef WINDOWS
|
---|
393 | # ifndef CR_NEWWINTRACK
|
---|
394 | stubUninstallWindowMessageHook();
|
---|
395 | # endif
|
---|
396 | #endif
|
---|
397 |
|
---|
398 | #if defined(CR_NEWWINTRACK)
|
---|
399 | crUnlockMutex(mutex);
|
---|
400 | # if defined(WINDOWS)
|
---|
401 | if (stub.hSyncThread && RTThreadGetState(stub.hSyncThread)!=RTTHREADSTATE_TERMINATED)
|
---|
402 | {
|
---|
403 | HANDLE hNative;
|
---|
404 | DWORD ec=0;
|
---|
405 |
|
---|
406 | hNative = OpenThread(SYNCHRONIZE|THREAD_QUERY_INFORMATION|THREAD_TERMINATE,
|
---|
407 | false, RTThreadGetNative(stub.hSyncThread));
|
---|
408 | if (!hNative)
|
---|
409 | {
|
---|
410 | crWarning("Failed to get handle for sync thread(%#x)", GetLastError());
|
---|
411 | }
|
---|
412 | else
|
---|
413 | {
|
---|
414 | crDebug("Got handle %p for thread %#x", hNative, RTThreadGetNative(stub.hSyncThread));
|
---|
415 | }
|
---|
416 |
|
---|
417 | ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
|
---|
418 |
|
---|
419 | if (PostThreadMessage(RTThreadGetNative(stub.hSyncThread), WM_QUIT, 0, 0))
|
---|
420 | {
|
---|
421 | RTThreadWait(stub.hSyncThread, 1000, NULL);
|
---|
422 |
|
---|
423 | /*Same issue as on linux, RTThreadWait exits before system thread is terminated, which leads
|
---|
424 | * to issues as our dll goes to be unloaded.
|
---|
425 | *@todo
|
---|
426 | *We usually call this function from DllMain which seems to be holding some lock and thus we have to
|
---|
427 | * kill thread via TerminateThread.
|
---|
428 | */
|
---|
429 | if (WaitForSingleObject(hNative, 100)==WAIT_TIMEOUT)
|
---|
430 | {
|
---|
431 | crDebug("Wait failed, terminating");
|
---|
432 | if (!TerminateThread(hNative, 1))
|
---|
433 | {
|
---|
434 | crDebug("TerminateThread failed");
|
---|
435 | }
|
---|
436 | }
|
---|
437 | if (GetExitCodeThread(hNative, &ec))
|
---|
438 | {
|
---|
439 | crDebug("Thread %p exited with ec=%i", hNative, ec);
|
---|
440 | }
|
---|
441 | else
|
---|
442 | {
|
---|
443 | crDebug("GetExitCodeThread failed(%#x)", GetLastError());
|
---|
444 | }
|
---|
445 | }
|
---|
446 | else
|
---|
447 | {
|
---|
448 | crDebug("Sync thread killed before DLL_PROCESS_DETACH");
|
---|
449 | }
|
---|
450 |
|
---|
451 | if (hNative)
|
---|
452 | {
|
---|
453 | CloseHandle(hNative);
|
---|
454 | }
|
---|
455 | }
|
---|
456 | #else
|
---|
457 | if (stub.hSyncThread!=NIL_RTTHREAD)
|
---|
458 | {
|
---|
459 | ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
|
---|
460 | {
|
---|
461 | /*RTThreadWait might return too early, which cause our code being unloaded while RT thread wrapper is still running*/
|
---|
462 | int rc = pthread_join(RTThreadGetNative(stub.hSyncThread), NULL);
|
---|
463 | if (!rc)
|
---|
464 | {
|
---|
465 | crDebug("pthread_join failed %i", rc);
|
---|
466 | }
|
---|
467 | }
|
---|
468 | }
|
---|
469 | #endif
|
---|
470 | crLockMutex(mutex);
|
---|
471 | #endif
|
---|
472 |
|
---|
473 | #ifndef WINDOWS
|
---|
474 | crNetTearDown();
|
---|
475 | #endif
|
---|
476 |
|
---|
477 | #ifdef CHROMIUM_THREADSAFE
|
---|
478 | crUnlockMutex(mutex);
|
---|
479 | crFreeMutex(mutex);
|
---|
480 | #endif
|
---|
481 | crMemset(&stub, 0, sizeof(stub));
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | static void stubExitHandler(void)
|
---|
486 | {
|
---|
487 | stubSPUSafeTearDown();
|
---|
488 | }
|
---|
489 |
|
---|
490 | /**
|
---|
491 | * Called when we receive a SIGTERM signal.
|
---|
492 | */
|
---|
493 | static void stubSignalHandler(int signo)
|
---|
494 | {
|
---|
495 | stubSPUSafeTearDown();
|
---|
496 | exit(0); /* this causes stubExitHandler() to be called */
|
---|
497 | }
|
---|
498 |
|
---|
499 | #ifndef RT_OS_WINDOWS
|
---|
500 | # ifdef CHROMIUM_THREADSAFE
|
---|
501 | static DECLCALLBACK(void) stubThreadTlsDtor(void *pvValue)
|
---|
502 | {
|
---|
503 | ContextInfo *pCtx = (ContextInfo*)pvValue;
|
---|
504 | VBoxTlsRefRelease(pCtx);
|
---|
505 | }
|
---|
506 | # endif
|
---|
507 | #endif
|
---|
508 |
|
---|
509 |
|
---|
510 | /**
|
---|
511 | * Init variables in the stub structure, install signal handler.
|
---|
512 | */
|
---|
513 | static void stubInitVars(void)
|
---|
514 | {
|
---|
515 | WindowInfo *defaultWin;
|
---|
516 |
|
---|
517 | #ifdef CHROMIUM_THREADSAFE
|
---|
518 | crInitMutex(&stub.mutex);
|
---|
519 | #endif
|
---|
520 |
|
---|
521 | /* At the very least we want CR_RGB_BIT. */
|
---|
522 | stub.haveNativeOpenGL = GL_FALSE;
|
---|
523 | stub.spu = NULL;
|
---|
524 | stub.appDrawCursor = 0;
|
---|
525 | stub.minChromiumWindowWidth = 0;
|
---|
526 | stub.minChromiumWindowHeight = 0;
|
---|
527 | stub.maxChromiumWindowWidth = 0;
|
---|
528 | stub.maxChromiumWindowHeight = 0;
|
---|
529 | stub.matchChromiumWindowCount = 0;
|
---|
530 | stub.matchChromiumWindowID = NULL;
|
---|
531 | stub.matchWindowTitle = NULL;
|
---|
532 | stub.ignoreFreeglutMenus = 0;
|
---|
533 | stub.threadSafe = GL_FALSE;
|
---|
534 | stub.trackWindowSize = 0;
|
---|
535 | stub.trackWindowPos = 0;
|
---|
536 | stub.trackWindowVisibility = 0;
|
---|
537 | stub.trackWindowVisibleRgn = 0;
|
---|
538 | stub.mothershipPID = 0;
|
---|
539 | stub.spu_dir = NULL;
|
---|
540 |
|
---|
541 | stub.freeContextNumber = MAGIC_CONTEXT_BASE;
|
---|
542 | stub.contextTable = crAllocHashtable();
|
---|
543 | #ifndef RT_OS_WINDOWS
|
---|
544 | # ifdef CHROMIUM_THREADSAFE
|
---|
545 | if (!g_stubIsCurrentContextTSDInited)
|
---|
546 | {
|
---|
547 | crInitTSDF(&g_stubCurrentContextTSD, stubThreadTlsDtor);
|
---|
548 | g_stubIsCurrentContextTSDInited = true;
|
---|
549 | }
|
---|
550 | # endif
|
---|
551 | #endif
|
---|
552 | stubSetCurrentContext(NULL);
|
---|
553 |
|
---|
554 | stub.windowTable = crAllocHashtable();
|
---|
555 |
|
---|
556 | #ifdef CR_NEWWINTRACK
|
---|
557 | stub.bShutdownSyncThread = false;
|
---|
558 | stub.hSyncThread = NIL_RTTHREAD;
|
---|
559 | #endif
|
---|
560 |
|
---|
561 | defaultWin = (WindowInfo *) crCalloc(sizeof(WindowInfo));
|
---|
562 | defaultWin->type = CHROMIUM;
|
---|
563 | defaultWin->spuWindow = 0; /* window 0 always exists */
|
---|
564 | #ifdef WINDOWS
|
---|
565 | defaultWin->hVisibleRegion = INVALID_HANDLE_VALUE;
|
---|
566 | #elif defined(GLX)
|
---|
567 | defaultWin->pVisibleRegions = NULL;
|
---|
568 | defaultWin->cVisibleRegions = 0;
|
---|
569 | #endif
|
---|
570 | crHashtableAdd(stub.windowTable, 0, defaultWin);
|
---|
571 |
|
---|
572 | #if 1
|
---|
573 | atexit(stubExitHandler);
|
---|
574 | signal(SIGTERM, stubSignalHandler);
|
---|
575 | signal(SIGINT, stubSignalHandler);
|
---|
576 | #ifndef WINDOWS
|
---|
577 | signal(SIGPIPE, SIG_IGN); /* the networking code should catch this */
|
---|
578 | #endif
|
---|
579 | #else
|
---|
580 | (void) stubExitHandler;
|
---|
581 | (void) stubSignalHandler;
|
---|
582 | #endif
|
---|
583 | }
|
---|
584 |
|
---|
585 |
|
---|
586 | /**
|
---|
587 | * Return a free port number for the mothership to use, or -1 if we
|
---|
588 | * can't find one.
|
---|
589 | */
|
---|
590 | static int
|
---|
591 | GenerateMothershipPort(void)
|
---|
592 | {
|
---|
593 | const int MAX_PORT = 10100;
|
---|
594 | unsigned short port;
|
---|
595 |
|
---|
596 | /* generate initial port number randomly */
|
---|
597 | crRandAutoSeed();
|
---|
598 | port = (unsigned short) crRandInt(10001, MAX_PORT);
|
---|
599 |
|
---|
600 | #ifdef WINDOWS
|
---|
601 | /* XXX should implement a free port check here */
|
---|
602 | return port;
|
---|
603 | #else
|
---|
604 | /*
|
---|
605 | * See if this port number really is free, try another if needed.
|
---|
606 | */
|
---|
607 | {
|
---|
608 | struct sockaddr_in servaddr;
|
---|
609 | int so_reuseaddr = 1;
|
---|
610 | int sock, k;
|
---|
611 |
|
---|
612 | /* create socket */
|
---|
613 | sock = socket(AF_INET, SOCK_STREAM, 0);
|
---|
614 | CRASSERT(sock > 2);
|
---|
615 |
|
---|
616 | /* deallocate socket/port when we exit */
|
---|
617 | k = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
---|
618 | (char *) &so_reuseaddr, sizeof(so_reuseaddr));
|
---|
619 | CRASSERT(k == 0);
|
---|
620 |
|
---|
621 | /* initialize the servaddr struct */
|
---|
622 | crMemset(&servaddr, 0, sizeof(servaddr) );
|
---|
623 | servaddr.sin_family = AF_INET;
|
---|
624 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
---|
625 |
|
---|
626 | while (port < MAX_PORT) {
|
---|
627 | /* Bind to the given port number, return -1 if we fail */
|
---|
628 | servaddr.sin_port = htons((unsigned short) port);
|
---|
629 | k = bind(sock, (struct sockaddr *) &servaddr, sizeof(servaddr));
|
---|
630 | if (k) {
|
---|
631 | /* failed to create port. try next one. */
|
---|
632 | port++;
|
---|
633 | }
|
---|
634 | else {
|
---|
635 | /* free the socket/port now so mothership can make it */
|
---|
636 | close(sock);
|
---|
637 | return port;
|
---|
638 | }
|
---|
639 | }
|
---|
640 | }
|
---|
641 | #endif /* WINDOWS */
|
---|
642 | return -1;
|
---|
643 | }
|
---|
644 |
|
---|
645 |
|
---|
646 | /**
|
---|
647 | * Try to determine which mothership configuration to use for this program.
|
---|
648 | */
|
---|
649 | static char **
|
---|
650 | LookupMothershipConfig(const char *procName)
|
---|
651 | {
|
---|
652 | const int procNameLen = crStrlen(procName);
|
---|
653 | FILE *f;
|
---|
654 | const char *home;
|
---|
655 | char configPath[1000];
|
---|
656 |
|
---|
657 | /* first, check if the CR_CONFIG env var is set */
|
---|
658 | {
|
---|
659 | const char *conf = crGetenv("CR_CONFIG");
|
---|
660 | if (conf && crStrlen(conf) > 0)
|
---|
661 | return crStrSplit(conf, " ");
|
---|
662 | }
|
---|
663 |
|
---|
664 | /* second, look up config name from config file */
|
---|
665 | home = crGetenv("HOME");
|
---|
666 | if (home)
|
---|
667 | sprintf(configPath, "%s/%s", home, CONFIG_LOOKUP_FILE);
|
---|
668 | else
|
---|
669 | crStrcpy(configPath, CONFIG_LOOKUP_FILE); /* from current dir */
|
---|
670 | /* Check if the CR_CONFIG_PATH env var is set. */
|
---|
671 | {
|
---|
672 | const char *conf = crGetenv("CR_CONFIG_PATH");
|
---|
673 | if (conf)
|
---|
674 | crStrcpy(configPath, conf); /* from env var */
|
---|
675 | }
|
---|
676 |
|
---|
677 | f = fopen(configPath, "r");
|
---|
678 | if (!f) {
|
---|
679 | return NULL;
|
---|
680 | }
|
---|
681 |
|
---|
682 | while (!feof(f)) {
|
---|
683 | char line[1000];
|
---|
684 | char **args;
|
---|
685 | fgets(line, 999, f);
|
---|
686 | line[crStrlen(line) - 1] = 0; /* remove trailing newline */
|
---|
687 | if (crStrncmp(line, procName, procNameLen) == 0 &&
|
---|
688 | (line[procNameLen] == ' ' || line[procNameLen] == '\t'))
|
---|
689 | {
|
---|
690 | crWarning("Using Chromium configuration for %s from %s",
|
---|
691 | procName, configPath);
|
---|
692 | args = crStrSplit(line + procNameLen + 1, " ");
|
---|
693 | return args;
|
---|
694 | }
|
---|
695 | }
|
---|
696 | fclose(f);
|
---|
697 | return NULL;
|
---|
698 | }
|
---|
699 |
|
---|
700 |
|
---|
701 | static int Mothership_Awake = 0;
|
---|
702 |
|
---|
703 |
|
---|
704 | /**
|
---|
705 | * Signal handler to determine when mothership is ready.
|
---|
706 | */
|
---|
707 | static void
|
---|
708 | MothershipPhoneHome(int signo)
|
---|
709 | {
|
---|
710 | crDebug("Got signal %d: mothership is awake!", signo);
|
---|
711 | Mothership_Awake = 1;
|
---|
712 | }
|
---|
713 |
|
---|
714 | void stubSetDefaultConfigurationOptions(void)
|
---|
715 | {
|
---|
716 | unsigned char key[16]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
|
---|
717 |
|
---|
718 | stub.appDrawCursor = 0;
|
---|
719 | stub.minChromiumWindowWidth = 0;
|
---|
720 | stub.minChromiumWindowHeight = 0;
|
---|
721 | stub.maxChromiumWindowWidth = 0;
|
---|
722 | stub.maxChromiumWindowHeight = 0;
|
---|
723 | stub.matchChromiumWindowID = NULL;
|
---|
724 | stub.numIgnoreWindowID = 0;
|
---|
725 | stub.matchWindowTitle = NULL;
|
---|
726 | stub.ignoreFreeglutMenus = 0;
|
---|
727 | stub.trackWindowSize = 1;
|
---|
728 | stub.trackWindowPos = 1;
|
---|
729 | stub.trackWindowVisibility = 1;
|
---|
730 | stub.trackWindowVisibleRgn = 1;
|
---|
731 | stub.matchChromiumWindowCount = 0;
|
---|
732 | stub.spu_dir = NULL;
|
---|
733 | crNetSetRank(0);
|
---|
734 | crNetSetContextRange(32, 35);
|
---|
735 | crNetSetNodeRange("iam0", "iamvis20");
|
---|
736 | crNetSetKey(key,sizeof(key));
|
---|
737 | stub.force_pbuffers = 0;
|
---|
738 |
|
---|
739 | #ifdef WINDOWS
|
---|
740 | # ifdef VBOX_WITH_WDDM
|
---|
741 | stub.bRunningUnderWDDM = false;
|
---|
742 | # endif
|
---|
743 | #endif
|
---|
744 | }
|
---|
745 |
|
---|
746 | #ifdef CR_NEWWINTRACK
|
---|
747 | # ifdef VBOX_WITH_WDDM
|
---|
748 | static stubDispatchVisibleRegions(WindowInfo *pWindow)
|
---|
749 | {
|
---|
750 | DWORD dwCount;
|
---|
751 | LPRGNDATA lpRgnData;
|
---|
752 |
|
---|
753 | dwCount = GetRegionData(pWindow->hVisibleRegion, 0, NULL);
|
---|
754 | lpRgnData = crAlloc(dwCount);
|
---|
755 |
|
---|
756 | if (lpRgnData)
|
---|
757 | {
|
---|
758 | GetRegionData(pWindow->hVisibleRegion, dwCount, lpRgnData);
|
---|
759 | crDebug("Dispatched WindowVisibleRegion (%i, cRects=%i)", pWindow->spuWindow, lpRgnData->rdh.nCount);
|
---|
760 | stub.spuDispatch.WindowVisibleRegion(pWindow->spuWindow, lpRgnData->rdh.nCount, (GLint*) lpRgnData->Buffer);
|
---|
761 | crFree(lpRgnData);
|
---|
762 | }
|
---|
763 | else crWarning("GetRegionData failed, VisibleRegions update failed");
|
---|
764 | }
|
---|
765 |
|
---|
766 | static HRGN stubMakeRegionFromRects(PVBOXVIDEOCM_CMD_RECTS pRegions, uint32_t start)
|
---|
767 | {
|
---|
768 | HRGN hRgn, hTmpRgn;
|
---|
769 | uint32_t i;
|
---|
770 |
|
---|
771 | if (pRegions->RectsInfo.cRects<=start)
|
---|
772 | {
|
---|
773 | return INVALID_HANDLE_VALUE;
|
---|
774 | }
|
---|
775 |
|
---|
776 | hRgn = CreateRectRgn(0, 0, 0, 0);
|
---|
777 | for (i=start; i<pRegions->RectsInfo.cRects; ++i)
|
---|
778 | {
|
---|
779 | hTmpRgn = CreateRectRgnIndirect(&pRegions->RectsInfo.aRects[i]);
|
---|
780 | CombineRgn(hRgn, hRgn, hTmpRgn, RGN_OR);
|
---|
781 | DeleteObject(hTmpRgn);
|
---|
782 | }
|
---|
783 | return hRgn;
|
---|
784 | }
|
---|
785 |
|
---|
786 | # endif /* VBOX_WITH_WDDM */
|
---|
787 |
|
---|
788 | static void stubSyncTrCheckWindowsCB(unsigned long key, void *data1, void *data2)
|
---|
789 | {
|
---|
790 | WindowInfo *pWindow = (WindowInfo *) data1;
|
---|
791 | (void) data2;
|
---|
792 |
|
---|
793 | if (pWindow->type!=CHROMIUM || pWindow->spuWindow==0)
|
---|
794 | {
|
---|
795 | return;
|
---|
796 | }
|
---|
797 |
|
---|
798 | stub.spu->dispatch_table.VBoxPackSetInjectID(pWindow->u32ClientID);
|
---|
799 |
|
---|
800 | if (!stubSystemWindowExist(pWindow))
|
---|
801 | {
|
---|
802 | #ifdef WINDOWS
|
---|
803 | stubDestroyWindow(0, (GLint)pWindow->hWnd);
|
---|
804 | #else
|
---|
805 | stubDestroyWindow(0, (GLint)pWindow->drawable);
|
---|
806 | #endif
|
---|
807 | /*No need to flush here as crWindowDestroy does it*/
|
---|
808 | return;
|
---|
809 | }
|
---|
810 |
|
---|
811 | #if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
|
---|
812 | if (stub.bRunningUnderWDDM)
|
---|
813 | return;
|
---|
814 | #endif
|
---|
815 | stubCheckWindowState(pWindow, GL_TRUE);
|
---|
816 | }
|
---|
817 |
|
---|
818 | static DECLCALLBACK(int) stubSyncThreadProc(RTTHREAD ThreadSelf, void *pvUser)
|
---|
819 | {
|
---|
820 | #ifdef WINDOWS
|
---|
821 | MSG msg;
|
---|
822 | # ifdef VBOX_WITH_WDDM
|
---|
823 | HMODULE hVBoxD3D = NULL;
|
---|
824 | GLint spuConnection = 0;
|
---|
825 | # endif
|
---|
826 | #endif
|
---|
827 |
|
---|
828 | (void) pvUser;
|
---|
829 |
|
---|
830 | crDebug("Sync thread started");
|
---|
831 | #ifdef WINDOWS
|
---|
832 | PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
|
---|
833 | # ifdef VBOX_WITH_WDDM
|
---|
834 | hVBoxD3D = NULL;
|
---|
835 | if (!GetModuleHandleEx(0, VBOX_MODNAME_DISPD3D, &hVBoxD3D))
|
---|
836 | {
|
---|
837 | crDebug("GetModuleHandleEx failed err %d", GetLastError());
|
---|
838 | hVBoxD3D = NULL;
|
---|
839 | }
|
---|
840 |
|
---|
841 | if (hVBoxD3D)
|
---|
842 | {
|
---|
843 | crDebug("running with " VBOX_MODNAME_DISPD3D);
|
---|
844 | stub.trackWindowVisibleRgn = 0;
|
---|
845 | stub.bRunningUnderWDDM = true;
|
---|
846 | }
|
---|
847 | # endif /* VBOX_WITH_WDDM */
|
---|
848 | #endif /* WINDOWS */
|
---|
849 |
|
---|
850 | crLockMutex(&stub.mutex);
|
---|
851 | #if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
|
---|
852 | spuConnection =
|
---|
853 | #endif
|
---|
854 | stub.spu->dispatch_table.VBoxPackSetInjectThread(NULL);
|
---|
855 | #if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
|
---|
856 | if (stub.bRunningUnderWDDM && !spuConnection)
|
---|
857 | {
|
---|
858 | crError("VBoxPackSetInjectThread failed!");
|
---|
859 | }
|
---|
860 | #endif
|
---|
861 | crUnlockMutex(&stub.mutex);
|
---|
862 |
|
---|
863 | RTThreadUserSignal(ThreadSelf);
|
---|
864 |
|
---|
865 | while(!stub.bShutdownSyncThread)
|
---|
866 | {
|
---|
867 | #ifdef WINDOWS
|
---|
868 | if (!PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
|
---|
869 | {
|
---|
870 | # ifdef VBOX_WITH_WDDM
|
---|
871 | if (stub.bRunningUnderWDDM)
|
---|
872 | {
|
---|
873 |
|
---|
874 | }
|
---|
875 | else
|
---|
876 | # endif
|
---|
877 | {
|
---|
878 | crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
|
---|
879 | RTThreadSleep(50);
|
---|
880 | }
|
---|
881 | }
|
---|
882 | else
|
---|
883 | {
|
---|
884 | if (WM_QUIT==msg.message)
|
---|
885 | {
|
---|
886 | crDebug("Sync thread got WM_QUIT");
|
---|
887 | break;
|
---|
888 | }
|
---|
889 | else
|
---|
890 | {
|
---|
891 | TranslateMessage(&msg);
|
---|
892 | DispatchMessage(&msg);
|
---|
893 | }
|
---|
894 | }
|
---|
895 | #else
|
---|
896 | /* Try to keep a consistent locking order. */
|
---|
897 | crHashtableLock(stub.windowTable);
|
---|
898 | crLockMutex(&stub.mutex);
|
---|
899 | crHashtableWalkUnlocked(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
|
---|
900 | crUnlockMutex(&stub.mutex);
|
---|
901 | crHashtableUnlock(stub.windowTable);
|
---|
902 | RTThreadSleep(50);
|
---|
903 | #endif
|
---|
904 | }
|
---|
905 |
|
---|
906 | #ifdef VBOX_WITH_WDDM
|
---|
907 | if (spuConnection)
|
---|
908 | {
|
---|
909 | stub.spu->dispatch_table.VBoxConDestroy(spuConnection);
|
---|
910 | }
|
---|
911 | if (hVBoxD3D)
|
---|
912 | {
|
---|
913 | FreeLibrary(hVBoxD3D);
|
---|
914 | }
|
---|
915 | #endif
|
---|
916 | crDebug("Sync thread stopped");
|
---|
917 | return 0;
|
---|
918 | }
|
---|
919 | #endif /* CR_NEWWINTRACK */
|
---|
920 |
|
---|
921 | /**
|
---|
922 | * Do one-time initializations for the faker.
|
---|
923 | * Returns TRUE on success, FALSE otherwise.
|
---|
924 | */
|
---|
925 | static bool
|
---|
926 | stubInitLocked(void)
|
---|
927 | {
|
---|
928 | /* Here is where we contact the mothership to find out what we're supposed
|
---|
929 | * to be doing. Networking code in a DLL initializer. I sure hope this
|
---|
930 | * works :)
|
---|
931 | *
|
---|
932 | * HOW can I pass the mothership address to this if I already know it?
|
---|
933 | */
|
---|
934 |
|
---|
935 | CRConnection *conn = NULL;
|
---|
936 | char response[1024];
|
---|
937 | char **spuchain;
|
---|
938 | int num_spus;
|
---|
939 | int *spu_ids;
|
---|
940 | char **spu_names;
|
---|
941 | const char *app_id;
|
---|
942 | int i;
|
---|
943 | int disable_sync = 0;
|
---|
944 | #if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
|
---|
945 | HMODULE hVBoxD3D = NULL;
|
---|
946 | #endif
|
---|
947 |
|
---|
948 | stubInitVars();
|
---|
949 |
|
---|
950 | crGetProcName(response, 1024);
|
---|
951 | crDebug("Stub launched for %s", response);
|
---|
952 |
|
---|
953 | #if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
|
---|
954 | /*@todo when vm boots with compiz turned on, new code causes hang in xcb_wait_for_reply in the sync thread
|
---|
955 | * as at the start compiz runs our code under XGrabServer.
|
---|
956 | */
|
---|
957 | if (!crStrcmp(response, "compiz") || !crStrcmp(response, "compiz_real") || !crStrcmp(response, "compiz.real")
|
---|
958 | || !crStrcmp(response, "compiz-bin"))
|
---|
959 | {
|
---|
960 | disable_sync = 1;
|
---|
961 | }
|
---|
962 | #endif
|
---|
963 |
|
---|
964 | /* @todo check if it'd be of any use on other than guests, no use for windows */
|
---|
965 | app_id = crGetenv( "CR_APPLICATION_ID_NUMBER" );
|
---|
966 |
|
---|
967 | crNetInit( NULL, NULL );
|
---|
968 |
|
---|
969 | #ifndef WINDOWS
|
---|
970 | {
|
---|
971 | CRNetServer ns;
|
---|
972 |
|
---|
973 | ns.name = "vboxhgcm://host:0";
|
---|
974 | ns.buffer_size = 1024;
|
---|
975 | crNetServerConnect(&ns
|
---|
976 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
977 | , NULL
|
---|
978 | #endif
|
---|
979 | );
|
---|
980 | if (!ns.conn)
|
---|
981 | {
|
---|
982 | crWarning("Failed to connect to host. Make sure 3D acceleration is enabled for this VM.");
|
---|
983 | return false;
|
---|
984 | }
|
---|
985 | else
|
---|
986 | {
|
---|
987 | crNetFreeConnection(ns.conn);
|
---|
988 | }
|
---|
989 | }
|
---|
990 | #endif
|
---|
991 |
|
---|
992 | strcpy(response, "2 0 feedback 1 pack");
|
---|
993 | spuchain = crStrSplit( response, " " );
|
---|
994 | num_spus = crStrToInt( spuchain[0] );
|
---|
995 | spu_ids = (int *) crAlloc( num_spus * sizeof( *spu_ids ) );
|
---|
996 | spu_names = (char **) crAlloc( num_spus * sizeof( *spu_names ) );
|
---|
997 | for (i = 0 ; i < num_spus ; i++)
|
---|
998 | {
|
---|
999 | spu_ids[i] = crStrToInt( spuchain[2*i+1] );
|
---|
1000 | spu_names[i] = crStrdup( spuchain[2*i+2] );
|
---|
1001 | crDebug( "SPU %d/%d: (%d) \"%s\"", i+1, num_spus, spu_ids[i], spu_names[i] );
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | stubSetDefaultConfigurationOptions();
|
---|
1005 |
|
---|
1006 | #if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
|
---|
1007 | hVBoxD3D = NULL;
|
---|
1008 | if (!GetModuleHandleEx(0, VBOX_MODNAME_DISPD3D, &hVBoxD3D))
|
---|
1009 | {
|
---|
1010 | crDebug("GetModuleHandleEx failed err %d", GetLastError());
|
---|
1011 | hVBoxD3D = NULL;
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | if (hVBoxD3D)
|
---|
1015 | {
|
---|
1016 | disable_sync = 1;
|
---|
1017 | crDebug("running with %s", VBOX_MODNAME_DISPD3D);
|
---|
1018 | stub.trackWindowVisibleRgn = 0;
|
---|
1019 | /* @todo: should we enable that? */
|
---|
1020 | stub.trackWindowSize = 0;
|
---|
1021 | stub.trackWindowPos = 0;
|
---|
1022 | stub.trackWindowVisibility = 0;
|
---|
1023 | stub.bRunningUnderWDDM = true;
|
---|
1024 | }
|
---|
1025 | #endif
|
---|
1026 |
|
---|
1027 | stub.spu = crSPULoadChain( num_spus, spu_ids, spu_names, stub.spu_dir, NULL );
|
---|
1028 |
|
---|
1029 | crFree( spuchain );
|
---|
1030 | crFree( spu_ids );
|
---|
1031 | for (i = 0; i < num_spus; ++i)
|
---|
1032 | crFree(spu_names[i]);
|
---|
1033 | crFree( spu_names );
|
---|
1034 |
|
---|
1035 | // spu chain load failed somewhere
|
---|
1036 | if (!stub.spu) {
|
---|
1037 | return false;
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | crSPUInitDispatchTable( &glim );
|
---|
1041 |
|
---|
1042 | /* This is unlikely to change -- We still want to initialize our dispatch
|
---|
1043 | * table with the functions of the first SPU in the chain. */
|
---|
1044 | stubInitSPUDispatch( stub.spu );
|
---|
1045 |
|
---|
1046 | /* we need to plug one special stub function into the dispatch table */
|
---|
1047 | glim.GetChromiumParametervCR = stub_GetChromiumParametervCR;
|
---|
1048 |
|
---|
1049 | #if !defined(VBOX_NO_NATIVEGL)
|
---|
1050 | /* Load pointers to native OpenGL functions into stub.nativeDispatch */
|
---|
1051 | stubInitNativeDispatch();
|
---|
1052 | #endif
|
---|
1053 |
|
---|
1054 | /*crDebug("stub init");
|
---|
1055 | raise(SIGINT);*/
|
---|
1056 |
|
---|
1057 | #ifdef WINDOWS
|
---|
1058 | # ifndef CR_NEWWINTRACK
|
---|
1059 | stubInstallWindowMessageHook();
|
---|
1060 | # endif
|
---|
1061 | #endif
|
---|
1062 |
|
---|
1063 | #ifdef CR_NEWWINTRACK
|
---|
1064 | {
|
---|
1065 | int rc;
|
---|
1066 |
|
---|
1067 | RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
|
---|
1068 |
|
---|
1069 | if (!disable_sync)
|
---|
1070 | {
|
---|
1071 | crDebug("Starting sync thread");
|
---|
1072 |
|
---|
1073 | rc = RTThreadCreate(&stub.hSyncThread, stubSyncThreadProc, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "Sync");
|
---|
1074 | if (RT_FAILURE(rc))
|
---|
1075 | {
|
---|
1076 | crError("Failed to start sync thread! (%x)", rc);
|
---|
1077 | }
|
---|
1078 | RTThreadUserWait(stub.hSyncThread, 60 * 1000);
|
---|
1079 | RTThreadUserReset(stub.hSyncThread);
|
---|
1080 |
|
---|
1081 | crDebug("Going on");
|
---|
1082 | }
|
---|
1083 | }
|
---|
1084 | #endif
|
---|
1085 |
|
---|
1086 | #ifdef GLX
|
---|
1087 | stub.xshmSI.shmid = -1;
|
---|
1088 | stub.bShmInitFailed = GL_FALSE;
|
---|
1089 | stub.pGLXPixmapsHash = crAllocHashtable();
|
---|
1090 |
|
---|
1091 | stub.bXExtensionsChecked = GL_FALSE;
|
---|
1092 | stub.bHaveXComposite = GL_FALSE;
|
---|
1093 | stub.bHaveXFixes = GL_FALSE;
|
---|
1094 | #endif
|
---|
1095 |
|
---|
1096 | return true;
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | /**
|
---|
1100 | * Do one-time initializations for the faker.
|
---|
1101 | * Returns TRUE on success, FALSE otherwise.
|
---|
1102 | */
|
---|
1103 | bool
|
---|
1104 | stubInit(void)
|
---|
1105 | {
|
---|
1106 | bool bRc = true;
|
---|
1107 | /* we need to serialize the initialization, otherwise racing is possible
|
---|
1108 | * for XPDM-based d3d when a d3d switcher is testing the gl lib in two or more threads
|
---|
1109 | * NOTE: the STUB_INIT_LOCK/UNLOCK is a NOP for non-win currently */
|
---|
1110 | STUB_INIT_LOCK();
|
---|
1111 | if (!stub_initialized)
|
---|
1112 | bRc = stub_initialized = stubInitLocked();
|
---|
1113 | STUB_INIT_UNLOCK();
|
---|
1114 | return bRc;
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | /* Sigh -- we can't do initialization at load time, since Windows forbids
|
---|
1118 | * the loading of other libraries from DLLMain. */
|
---|
1119 |
|
---|
1120 | #ifdef LINUX
|
---|
1121 | /* GCC crap
|
---|
1122 | *void (*stub_init_ptr)(void) __attribute__((section(".ctors"))) = __stubInit; */
|
---|
1123 | #endif
|
---|
1124 |
|
---|
1125 | #ifdef WINDOWS
|
---|
1126 | #define WIN32_LEAN_AND_MEAN
|
---|
1127 | #include <windows.h>
|
---|
1128 |
|
---|
1129 | #if 1//def DEBUG_misha
|
---|
1130 | /* debugging: this is to be able to catch first-chance notifications
|
---|
1131 | * for exceptions other than EXCEPTION_BREAKPOINT in kernel debugger */
|
---|
1132 | # define VDBG_VEHANDLER
|
---|
1133 | #endif
|
---|
1134 |
|
---|
1135 | #ifdef VDBG_VEHANDLER
|
---|
1136 | # include <dbghelp.h>
|
---|
1137 | static PVOID g_VBoxVehHandler = NULL;
|
---|
1138 | static DWORD g_VBoxVehEnable = 0;
|
---|
1139 |
|
---|
1140 | /* generate a crash dump on exception */
|
---|
1141 | #define VBOXVEH_F_DUMP 0x00000001
|
---|
1142 | /* generate a debugger breakpoint exception */
|
---|
1143 | #define VBOXVEH_F_BREAK 0x00000002
|
---|
1144 | /* exit on exception */
|
---|
1145 | #define VBOXVEH_F_EXIT 0x00000004
|
---|
1146 |
|
---|
1147 | static DWORD g_VBoxVehFlags = 0
|
---|
1148 | #ifdef DEBUG_misha
|
---|
1149 | | VBOXVEH_F_BREAK
|
---|
1150 | #else
|
---|
1151 | | VBOXVEH_F_DUMP
|
---|
1152 | #endif
|
---|
1153 | ;
|
---|
1154 |
|
---|
1155 | typedef BOOL WINAPI FNVBOXDBG_MINIDUMPWRITEDUMP(HANDLE hProcess,
|
---|
1156 | DWORD ProcessId,
|
---|
1157 | HANDLE hFile,
|
---|
1158 | MINIDUMP_TYPE DumpType,
|
---|
1159 | PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
|
---|
1160 | PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
|
---|
1161 | PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
|
---|
1162 | typedef FNVBOXDBG_MINIDUMPWRITEDUMP *PFNVBOXDBG_MINIDUMPWRITEDUMP;
|
---|
1163 |
|
---|
1164 | static HMODULE g_hVBoxMdDbgHelp = NULL;
|
---|
1165 | static PFNVBOXDBG_MINIDUMPWRITEDUMP g_pfnVBoxMdMiniDumpWriteDump = NULL;
|
---|
1166 | static uint32_t g_cVBoxMdFilePrefixLen = 0;
|
---|
1167 | static WCHAR g_aszwVBoxMdFilePrefix[MAX_PATH];
|
---|
1168 | static WCHAR g_aszwVBoxMdDumpCount = 0;
|
---|
1169 | static MINIDUMP_TYPE g_enmVBoxMdDumpType = MiniDumpNormal
|
---|
1170 | | MiniDumpWithDataSegs
|
---|
1171 | | MiniDumpWithFullMemory
|
---|
1172 | | MiniDumpWithHandleData
|
---|
1173 | //// | MiniDumpFilterMemory
|
---|
1174 | //// | MiniDumpScanMemory
|
---|
1175 | // | MiniDumpWithUnloadedModules
|
---|
1176 | //// | MiniDumpWithIndirectlyReferencedMemory
|
---|
1177 | //// | MiniDumpFilterModulePaths
|
---|
1178 | // | MiniDumpWithProcessThreadData
|
---|
1179 | // | MiniDumpWithPrivateReadWriteMemory
|
---|
1180 | //// | MiniDumpWithoutOptionalData
|
---|
1181 | // | MiniDumpWithFullMemoryInfo
|
---|
1182 | // | MiniDumpWithThreadInfo
|
---|
1183 | // | MiniDumpWithCodeSegs
|
---|
1184 | // | MiniDumpWithFullAuxiliaryState
|
---|
1185 | // | MiniDumpWithPrivateWriteCopyMemory
|
---|
1186 | // | MiniDumpIgnoreInaccessibleMemory
|
---|
1187 | // | MiniDumpWithTokenInformation
|
---|
1188 | //// | MiniDumpWithModuleHeaders
|
---|
1189 | //// | MiniDumpFilterTriage
|
---|
1190 | ;
|
---|
1191 |
|
---|
1192 |
|
---|
1193 |
|
---|
1194 | #define VBOXMD_DUMP_DIR_PREFIX_DEFAULT L"C:\\dumps\\vboxdmp"
|
---|
1195 |
|
---|
1196 | static HMODULE loadSystemDll(const char *pszName)
|
---|
1197 | {
|
---|
1198 | char szPath[MAX_PATH];
|
---|
1199 | UINT cchPath = GetSystemDirectoryA(szPath, sizeof(szPath));
|
---|
1200 | size_t cbName = strlen(pszName) + 1;
|
---|
1201 | if (cchPath + 1 + cbName > sizeof(szPath))
|
---|
1202 | {
|
---|
1203 | SetLastError(ERROR_FILENAME_EXCED_RANGE);
|
---|
1204 | return NULL;
|
---|
1205 | }
|
---|
1206 | szPath[cchPath] = '\\';
|
---|
1207 | memcpy(&szPath[cchPath + 1], pszName, cbName);
|
---|
1208 | return LoadLibraryA(szPath);
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | static DWORD vboxMdMinidumpCreate(struct _EXCEPTION_POINTERS *pExceptionInfo)
|
---|
1212 | {
|
---|
1213 | WCHAR aszwMdFileName[MAX_PATH];
|
---|
1214 | HANDLE hProcess = GetCurrentProcess();
|
---|
1215 | DWORD ProcessId = GetCurrentProcessId();
|
---|
1216 | MINIDUMP_EXCEPTION_INFORMATION ExceptionInfo;
|
---|
1217 | HANDLE hFile;
|
---|
1218 | DWORD winErr = ERROR_SUCCESS;
|
---|
1219 |
|
---|
1220 | if (!g_pfnVBoxMdMiniDumpWriteDump)
|
---|
1221 | {
|
---|
1222 | if (!g_hVBoxMdDbgHelp)
|
---|
1223 | {
|
---|
1224 | g_hVBoxMdDbgHelp = loadSystemDll("DbgHelp.dll");
|
---|
1225 | if (!g_hVBoxMdDbgHelp)
|
---|
1226 | return GetLastError();
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | g_pfnVBoxMdMiniDumpWriteDump = (PFNVBOXDBG_MINIDUMPWRITEDUMP)GetProcAddress(g_hVBoxMdDbgHelp, "MiniDumpWriteDump");
|
---|
1230 | if (!g_pfnVBoxMdMiniDumpWriteDump)
|
---|
1231 | return GetLastError();
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | /* @todo: this is a tmp stuff until we get that info from the settings properly */
|
---|
1235 | if (!g_cVBoxMdFilePrefixLen)
|
---|
1236 | {
|
---|
1237 | g_cVBoxMdFilePrefixLen = sizeof (VBOXMD_DUMP_DIR_PREFIX_DEFAULT)/sizeof (g_aszwVBoxMdFilePrefix[0]) - 1 /* <- don't include nul terminator */;
|
---|
1238 | memcpy(g_aszwVBoxMdFilePrefix, VBOXMD_DUMP_DIR_PREFIX_DEFAULT, sizeof (VBOXMD_DUMP_DIR_PREFIX_DEFAULT));
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 |
|
---|
1242 | if (RT_ELEMENTS(aszwMdFileName) <= g_cVBoxMdFilePrefixLen)
|
---|
1243 | {
|
---|
1244 | return ERROR_INVALID_STATE;
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 | ++g_aszwVBoxMdDumpCount;
|
---|
1248 |
|
---|
1249 | memcpy(aszwMdFileName, g_aszwVBoxMdFilePrefix, g_cVBoxMdFilePrefixLen * sizeof (g_aszwVBoxMdFilePrefix[0]));
|
---|
1250 | swprintf(aszwMdFileName + g_cVBoxMdFilePrefixLen, RT_ELEMENTS(aszwMdFileName) - g_cVBoxMdFilePrefixLen, L"%d_%d.dmp", ProcessId, g_aszwVBoxMdDumpCount);
|
---|
1251 |
|
---|
1252 | hFile = CreateFileW(aszwMdFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
---|
1253 | if (hFile == INVALID_HANDLE_VALUE)
|
---|
1254 | return GetLastError();
|
---|
1255 |
|
---|
1256 | ExceptionInfo.ThreadId = GetCurrentThreadId();
|
---|
1257 | ExceptionInfo.ExceptionPointers = pExceptionInfo;
|
---|
1258 | ExceptionInfo.ClientPointers = FALSE;
|
---|
1259 |
|
---|
1260 | if (!g_pfnVBoxMdMiniDumpWriteDump(hProcess, ProcessId, hFile, g_enmVBoxMdDumpType, &ExceptionInfo, NULL, NULL))
|
---|
1261 | winErr = GetLastError();
|
---|
1262 |
|
---|
1263 | CloseHandle(hFile);
|
---|
1264 | return winErr;
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 | LONG WINAPI vboxVDbgVectoredHandler(struct _EXCEPTION_POINTERS *pExceptionInfo)
|
---|
1268 | {
|
---|
1269 | PEXCEPTION_RECORD pExceptionRecord = pExceptionInfo->ExceptionRecord;
|
---|
1270 | PCONTEXT pContextRecord = pExceptionInfo->ContextRecord;
|
---|
1271 | switch (pExceptionRecord->ExceptionCode)
|
---|
1272 | {
|
---|
1273 | case EXCEPTION_BREAKPOINT:
|
---|
1274 | case EXCEPTION_ACCESS_VIOLATION:
|
---|
1275 | case EXCEPTION_STACK_OVERFLOW:
|
---|
1276 | case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
|
---|
1277 | case EXCEPTION_FLT_DIVIDE_BY_ZERO:
|
---|
1278 | case EXCEPTION_FLT_INVALID_OPERATION:
|
---|
1279 | case EXCEPTION_INT_DIVIDE_BY_ZERO:
|
---|
1280 | case EXCEPTION_ILLEGAL_INSTRUCTION:
|
---|
1281 | if (g_VBoxVehFlags & VBOXVEH_F_BREAK)
|
---|
1282 | {
|
---|
1283 | BOOL fBreak = TRUE;
|
---|
1284 | #ifndef DEBUG_misha
|
---|
1285 | if (pExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
|
---|
1286 | {
|
---|
1287 | HANDLE hProcess = GetCurrentProcess();
|
---|
1288 | BOOL fDebuggerPresent = FALSE;
|
---|
1289 | /* we do not want to generate breakpoint exceptions recursively, so do it only when running under debugger */
|
---|
1290 | if (CheckRemoteDebuggerPresent(hProcess, &fDebuggerPresent))
|
---|
1291 | fBreak = !!fDebuggerPresent;
|
---|
1292 | else
|
---|
1293 | fBreak = FALSE; /* <- the function has failed, don't break for sanity */
|
---|
1294 | }
|
---|
1295 | #endif
|
---|
1296 |
|
---|
1297 | if (fBreak)
|
---|
1298 | {
|
---|
1299 | RT_BREAKPOINT();
|
---|
1300 | }
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | if (g_VBoxVehFlags & VBOXVEH_F_DUMP)
|
---|
1304 | vboxMdMinidumpCreate(pExceptionInfo);
|
---|
1305 |
|
---|
1306 | if (g_VBoxVehFlags & VBOXVEH_F_EXIT)
|
---|
1307 | exit(1);
|
---|
1308 | break;
|
---|
1309 | default:
|
---|
1310 | break;
|
---|
1311 | }
|
---|
1312 | return EXCEPTION_CONTINUE_SEARCH;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | void vboxVDbgVEHandlerRegister()
|
---|
1316 | {
|
---|
1317 | CRASSERT(!g_VBoxVehHandler);
|
---|
1318 | g_VBoxVehHandler = AddVectoredExceptionHandler(1,vboxVDbgVectoredHandler);
|
---|
1319 | CRASSERT(g_VBoxVehHandler);
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | void vboxVDbgVEHandlerUnregister()
|
---|
1323 | {
|
---|
1324 | ULONG uResult;
|
---|
1325 | if (g_VBoxVehHandler)
|
---|
1326 | {
|
---|
1327 | uResult = RemoveVectoredExceptionHandler(g_VBoxVehHandler);
|
---|
1328 | CRASSERT(uResult);
|
---|
1329 | g_VBoxVehHandler = NULL;
|
---|
1330 | }
|
---|
1331 | }
|
---|
1332 | #endif
|
---|
1333 |
|
---|
1334 | /* Windows crap */
|
---|
1335 | BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
|
---|
1336 | {
|
---|
1337 | (void) lpvReserved;
|
---|
1338 |
|
---|
1339 | switch (fdwReason)
|
---|
1340 | {
|
---|
1341 | case DLL_PROCESS_ATTACH:
|
---|
1342 | {
|
---|
1343 | CRNetServer ns;
|
---|
1344 |
|
---|
1345 | #ifdef CHROMIUM_THREADSAFE
|
---|
1346 | crInitTSD(&g_stubCurrentContextTSD);
|
---|
1347 | #endif
|
---|
1348 |
|
---|
1349 | crInitMutex(&stub_init_mutex);
|
---|
1350 |
|
---|
1351 | #ifdef VDBG_VEHANDLER
|
---|
1352 | g_VBoxVehEnable = !!crGetenv("CR_DBG_VEH_ENABLE");
|
---|
1353 | # ifdef DEBUG_misha
|
---|
1354 | g_VBoxVehEnable = 1;
|
---|
1355 | # endif
|
---|
1356 | if (g_VBoxVehEnable)
|
---|
1357 | vboxVDbgVEHandlerRegister();
|
---|
1358 | #endif
|
---|
1359 |
|
---|
1360 | crNetInit(NULL, NULL);
|
---|
1361 | ns.name = "vboxhgcm://host:0";
|
---|
1362 | ns.buffer_size = 1024;
|
---|
1363 | crNetServerConnect(&ns
|
---|
1364 | #if defined(VBOX_WITH_CRHGSMI) && defined(IN_GUEST)
|
---|
1365 | , NULL
|
---|
1366 | #endif
|
---|
1367 | );
|
---|
1368 | if (!ns.conn)
|
---|
1369 | {
|
---|
1370 | crDebug("Failed to connect to host (is guest 3d acceleration enabled?), aborting ICD load.");
|
---|
1371 | #ifdef VDBG_VEHANDLER
|
---|
1372 | if (g_VBoxVehEnable)
|
---|
1373 | vboxVDbgVEHandlerUnregister();
|
---|
1374 | #endif
|
---|
1375 | return FALSE;
|
---|
1376 | }
|
---|
1377 | else
|
---|
1378 | {
|
---|
1379 | crNetFreeConnection(ns.conn);
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | break;
|
---|
1383 | }
|
---|
1384 |
|
---|
1385 | case DLL_PROCESS_DETACH:
|
---|
1386 | {
|
---|
1387 | /* do exactly the same thing as for DLL_THREAD_DETACH since
|
---|
1388 | * DLL_THREAD_DETACH is not called for the thread doing DLL_PROCESS_DETACH according to msdn docs */
|
---|
1389 | stubSetCurrentContext(NULL);
|
---|
1390 | if (stub_initialized)
|
---|
1391 | {
|
---|
1392 | CRASSERT(stub.spu);
|
---|
1393 | stub.spu->dispatch_table.VBoxDetachThread();
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 | stubSPUSafeTearDown();
|
---|
1397 |
|
---|
1398 | #ifdef CHROMIUM_THREADSAFE
|
---|
1399 | crFreeTSD(&g_stubCurrentContextTSD);
|
---|
1400 | #endif
|
---|
1401 |
|
---|
1402 | #ifdef VDBG_VEHANDLER
|
---|
1403 | if (g_VBoxVehEnable)
|
---|
1404 | vboxVDbgVEHandlerUnregister();
|
---|
1405 | #endif
|
---|
1406 | break;
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | case DLL_THREAD_ATTACH:
|
---|
1410 | {
|
---|
1411 | if (stub_initialized)
|
---|
1412 | {
|
---|
1413 | CRASSERT(stub.spu);
|
---|
1414 | stub.spu->dispatch_table.VBoxAttachThread();
|
---|
1415 | }
|
---|
1416 | break;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | case DLL_THREAD_DETACH:
|
---|
1420 | {
|
---|
1421 | stubSetCurrentContext(NULL);
|
---|
1422 | if (stub_initialized)
|
---|
1423 | {
|
---|
1424 | CRASSERT(stub.spu);
|
---|
1425 | stub.spu->dispatch_table.VBoxDetachThread();
|
---|
1426 | }
|
---|
1427 | break;
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 | default:
|
---|
1431 | break;
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | return TRUE;
|
---|
1435 | }
|
---|
1436 | #endif
|
---|