VirtualBox

source: vbox/trunk/src/VBox/Additions/common/crOpenGL/load.c@ 32394

最後變更 在這個檔案從32394是 32241,由 vboxsync 提交於 14 年 前

wddm: 1. guest installler integration (uninstall needs additional fixing), 2. dev/vga saved state fixes, 3. better split video dma and wddm 4. separate wddm wine libs, 5. etc.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 31.0 KB
 
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#ifdef CHROMIUM_THREADSAFE
30#include "cr_threads.h"
31#endif
32
33#ifdef VBOX_WITH_WDDM
34#include <d3d9types.h>
35#include <D3dumddi.h>
36#include "../../WINNT/Graphics/Miniport/wddm/VBoxVideoIf.h"
37#include "../../WINNT/Graphics/Display/wddm/vboxdispmp.h"
38#endif
39
40/**
41 * If you change this, see the comments in tilesortspu_context.c
42 */
43#define MAGIC_CONTEXT_BASE 500
44
45#define CONFIG_LOOKUP_FILE ".crconfigs"
46
47#ifdef WINDOWS
48#define PYTHON_EXE "python.exe"
49#else
50#define PYTHON_EXE "python"
51#endif
52
53#ifdef WINDOWS
54static char* gsViewportHackApps[] = {"googleearth.exe", NULL};
55#endif
56
57static int stub_initialized = 0;
58
59/* NOTE: 'SPUDispatchTable glim' is declared in NULLfuncs.py now */
60/* NOTE: 'SPUDispatchTable stubThreadsafeDispatch' is declared in tsfuncs.c */
61Stub stub;
62
63
64static void stubInitNativeDispatch( void )
65{
66#define MAX_FUNCS 1000
67 SPUNamedFunctionTable gl_funcs[MAX_FUNCS];
68 int numFuncs;
69
70 numFuncs = crLoadOpenGL( &stub.wsInterface, gl_funcs );
71
72 stub.haveNativeOpenGL = (numFuncs > 0);
73
74 /* XXX call this after context binding */
75 numFuncs += crLoadOpenGLExtensions( &stub.wsInterface, gl_funcs + numFuncs );
76
77 CRASSERT(numFuncs < MAX_FUNCS);
78
79 crSPUInitDispatchTable( &stub.nativeDispatch );
80 crSPUInitDispatch( &stub.nativeDispatch, gl_funcs );
81 crSPUInitDispatchNops( &stub.nativeDispatch );
82#undef MAX_FUNCS
83}
84
85
86/** Pointer to the SPU's real glClear and glViewport functions */
87static ClearFunc_t origClear;
88static ViewportFunc_t origViewport;
89static SwapBuffersFunc_t origSwapBuffers;
90static DrawBufferFunc_t origDrawBuffer;
91static ScissorFunc_t origScissor;
92
93static void stubCheckWindowState(WindowInfo *window, GLboolean bFlushOnChange)
94{
95 bool bForceUpdate = false;
96 bool bChanged = false;
97
98#ifdef WINDOWS
99 /* @todo install hook and track for WM_DISPLAYCHANGE */
100 {
101 DEVMODE devMode;
102
103 devMode.dmSize = sizeof(DEVMODE);
104 EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode);
105
106 if (devMode.dmPelsWidth!=window->dmPelsWidth || devMode.dmPelsHeight!=window->dmPelsHeight)
107 {
108 crDebug("Resolution changed(%d,%d), forcing window Pos/Size update", devMode.dmPelsWidth, devMode.dmPelsHeight);
109 window->dmPelsWidth = devMode.dmPelsWidth;
110 window->dmPelsHeight = devMode.dmPelsHeight;
111 bForceUpdate = true;
112 }
113 }
114#endif
115
116 bChanged = stubUpdateWindowGeometry(window, bForceUpdate) || bForceUpdate;
117
118#if defined(GLX) || defined (WINDOWS)
119 if (stub.trackWindowVisibleRgn)
120 {
121 bChanged = stubUpdateWindowVisibileRegions(window) || bChanged;
122 }
123#endif
124
125 if (stub.trackWindowVisibility && window->type == CHROMIUM && window->drawable) {
126 const int mapped = stubIsWindowVisible(window);
127 if (mapped != window->mapped) {
128 crDebug("Dispatched: WindowShow(%i, %i)", window->spuWindow, mapped);
129 stub.spu->dispatch_table.WindowShow(window->spuWindow, mapped);
130 window->mapped = mapped;
131 bChanged = true;
132 }
133 }
134
135 if (bFlushOnChange && bChanged)
136 {
137 stub.spu->dispatch_table.Flush();
138 }
139}
140
141static bool stubSystemWindowExist(WindowInfo *pWindow)
142{
143#ifdef WINDOWS
144 if (!WindowFromDC(pWindow->drawable))
145 {
146 return false;
147 }
148#else
149 Window root;
150 int x, y;
151 unsigned int border, depth, w, h;
152 Display *dpy;
153
154 dpy = stubGetWindowDisplay(pWindow);
155
156 XLOCK(dpy);
157 if (!XGetGeometry(dpy, pWindow->drawable, &root, &x, &y, &w, &h, &border, &depth))
158 {
159 XUNLOCK(dpy);
160 return false;
161 }
162 XUNLOCK(dpy);
163#endif
164
165 return true;
166}
167
168static void stubCheckWindowsCB(unsigned long key, void *data1, void *data2)
169{
170 WindowInfo *pWindow = (WindowInfo *) data1;
171 ContextInfo *pCtx = (ContextInfo *) data2;
172
173 if (pWindow == pCtx->currentDrawable
174 || pWindow->type!=CHROMIUM
175 || pWindow->pOwner!=pCtx)
176 {
177 return;
178 }
179
180 if (!stubSystemWindowExist(pWindow))
181 {
182#ifdef WINDOWS
183 crWindowDestroy((GLint)pWindow->hWnd);
184#else
185 crWindowDestroy((GLint)pWindow->drawable);
186#endif
187 return;
188 }
189
190 stubCheckWindowState(pWindow, GL_FALSE);
191}
192
193static void stubCheckWindowsState(void)
194{
195 CRASSERT(stub.trackWindowSize || stub.trackWindowPos);
196
197 if (!stub.currentContext)
198 return;
199
200#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
201 crLockMutex(&stub.mutex);
202#endif
203
204 stubCheckWindowState(stub.currentContext->currentDrawable, GL_TRUE);
205 crHashtableWalk(stub.windowTable, stubCheckWindowsCB, stub.currentContext);
206
207#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
208 crUnlockMutex(&stub.mutex);
209#endif
210}
211
212
213/**
214 * Override the head SPU's glClear function.
215 * We're basically trapping this function so that we can poll the
216 * application window size at a regular interval.
217 */
218static void SPU_APIENTRY trapClear(GLbitfield mask)
219{
220 stubCheckWindowsState();
221 /* call the original SPU glClear function */
222 origClear(mask);
223}
224
225/**
226 * As above, but for glViewport. Most apps call glViewport before
227 * glClear when a window is resized.
228 */
229static void SPU_APIENTRY trapViewport(GLint x, GLint y, GLsizei w, GLsizei h)
230{
231 stubCheckWindowsState();
232 /* call the original SPU glViewport function */
233 if (!stub.viewportHack)
234 {
235 origViewport(x, y, w, h);
236 }
237 else
238 {
239 int winX, winY;
240 unsigned int winW, winH;
241 WindowInfo *pWindow;
242 pWindow = stub.currentContext->currentDrawable;
243 stubGetWindowGeometry(pWindow, &winX, &winY, &winW, &winH);
244 origViewport(0, 0, winW, winH);
245 }
246}
247
248static void SPU_APIENTRY trapSwapBuffers(GLint window, GLint flags)
249{
250 stubCheckWindowsState();
251 origSwapBuffers(window, flags);
252}
253
254static void SPU_APIENTRY trapDrawBuffer(GLenum buf)
255{
256 stubCheckWindowsState();
257 origDrawBuffer(buf);
258}
259
260static void SPU_APIENTRY trapScissor(GLint x, GLint y, GLsizei w, GLsizei h)
261{
262 int winX, winY;
263 unsigned int winW, winH;
264 WindowInfo *pWindow;
265 pWindow = stub.currentContext->currentDrawable;
266 stubGetWindowGeometry(pWindow, &winX, &winY, &winW, &winH);
267 origScissor(0, 0, winW, winH);
268}
269
270/**
271 * Use the GL function pointers in <spu> to initialize the static glim
272 * dispatch table.
273 */
274static void stubInitSPUDispatch(SPU *spu)
275{
276 crSPUInitDispatchTable( &stub.spuDispatch );
277 crSPUCopyDispatchTable( &stub.spuDispatch, &(spu->dispatch_table) );
278
279 if (stub.trackWindowSize || stub.trackWindowPos || stub.trackWindowVisibleRgn) {
280 /* patch-in special glClear/Viewport function to track window sizing */
281 origClear = stub.spuDispatch.Clear;
282 origViewport = stub.spuDispatch.Viewport;
283 origSwapBuffers = stub.spuDispatch.SwapBuffers;
284 origDrawBuffer = stub.spuDispatch.DrawBuffer;
285 origScissor = stub.spuDispatch.Scissor;
286 stub.spuDispatch.Clear = trapClear;
287 stub.spuDispatch.Viewport = trapViewport;
288
289 if (stub.viewportHack)
290 stub.spuDispatch.Scissor = trapScissor;
291 /*stub.spuDispatch.SwapBuffers = trapSwapBuffers;
292 stub.spuDispatch.DrawBuffer = trapDrawBuffer;*/
293 }
294
295 crSPUCopyDispatchTable( &glim, &stub.spuDispatch );
296}
297
298// Callback function, used to destroy all created contexts
299static void hsWalkStubDestroyContexts(unsigned long key, void *data1, void *data2)
300{
301 stubDestroyContext(key);
302}
303
304/**
305 * This is called when we exit.
306 * We call all the SPU's cleanup functions.
307 */
308static void stubSPUTearDown(void)
309{
310 crDebug("stubSPUTearDown");
311 if (!stub_initialized) return;
312
313 stub_initialized = 0;
314
315#ifdef WINDOWS
316# ifndef CR_NEWWINTRACK
317 stubUninstallWindowMessageHook();
318# endif
319#endif
320
321#ifdef CR_NEWWINTRACK
322 ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
323#endif
324
325 //delete all created contexts
326 stubMakeCurrent( NULL, NULL);
327 crHashtableWalk(stub.contextTable, hsWalkStubDestroyContexts, NULL);
328
329 /* shutdown, now trap any calls to a NULL dispatcher */
330 crSPUCopyDispatchTable(&glim, &stubNULLDispatch);
331
332 crSPUUnloadChain(stub.spu);
333 stub.spu = NULL;
334
335#ifndef Linux
336 crUnloadOpenGL();
337#endif
338
339 crNetTearDown();
340
341#ifdef GLX
342 if (stub.xshmSI.shmid>=0)
343 {
344 shmctl(stub.xshmSI.shmid, IPC_RMID, 0);
345 shmdt(stub.xshmSI.shmaddr);
346 }
347 crFreeHashtable(stub.pGLXPixmapsHash, crFree);
348#endif
349
350 crFreeHashtable(stub.windowTable, crFree);
351 crFreeHashtable(stub.contextTable, NULL);
352
353 crMemset(&stub, 0, sizeof(stub));
354}
355
356static void stubSPUSafeTearDown(void)
357{
358#ifdef CHROMIUM_THREADSAFE
359 CRmutex *mutex;
360#endif
361
362 if (!stub_initialized) return;
363 stub_initialized = 0;
364
365#ifdef CHROMIUM_THREADSAFE
366 mutex = &stub.mutex;
367 crLockMutex(mutex);
368#endif
369 crDebug("stubSPUSafeTearDown");
370
371#ifdef WINDOWS
372# ifndef CR_NEWWINTRACK
373 stubUninstallWindowMessageHook();
374# endif
375#endif
376
377#if defined(CR_NEWWINTRACK)
378 crUnlockMutex(mutex);
379# if defined(WINDOWS)
380 if (RTThreadGetState(stub.hSyncThread)!=RTTHREADSTATE_TERMINATED)
381 {
382 ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
383 if (PostThreadMessage(RTThreadGetNative(stub.hSyncThread), WM_QUIT, 0, 0))
384 {
385 RTThreadWait(stub.hSyncThread, 1000, NULL);
386 }
387 else
388 {
389 crDebug("Sync thread killed before DLL_PROCESS_DETACH");
390 }
391 }
392#else
393 ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
394 {
395 /*RTThreadWait might return too early, which cause our code being unloaded while RT thread wrapper is still running*/
396 int rc = pthread_join(RTThreadGetNative(stub.hSyncThread), NULL);
397 if (!rc)
398 {
399 crDebug("pthread_join failed %i", rc);
400 }
401 }
402#endif
403 crLockMutex(mutex);
404#endif
405
406 crNetTearDown();
407#ifdef CHROMIUM_THREADSAFE
408 crUnlockMutex(mutex);
409 crFreeMutex(mutex);
410#endif
411 crMemset(&stub, 0, sizeof(stub));
412}
413
414
415static void stubExitHandler(void)
416{
417 stubSPUSafeTearDown();
418}
419
420/**
421 * Called when we receive a SIGTERM signal.
422 */
423static void stubSignalHandler(int signo)
424{
425 stubSPUSafeTearDown();
426 exit(0); /* this causes stubExitHandler() to be called */
427}
428
429
430/**
431 * Init variables in the stub structure, install signal handler.
432 */
433static void stubInitVars(void)
434{
435 WindowInfo *defaultWin;
436
437#ifdef CHROMIUM_THREADSAFE
438 crInitMutex(&stub.mutex);
439#endif
440
441 /* At the very least we want CR_RGB_BIT. */
442 stub.haveNativeOpenGL = GL_FALSE;
443 stub.spu = NULL;
444 stub.appDrawCursor = 0;
445 stub.minChromiumWindowWidth = 0;
446 stub.minChromiumWindowHeight = 0;
447 stub.maxChromiumWindowWidth = 0;
448 stub.maxChromiumWindowHeight = 0;
449 stub.matchChromiumWindowCount = 0;
450 stub.matchChromiumWindowID = NULL;
451 stub.matchWindowTitle = NULL;
452 stub.ignoreFreeglutMenus = 0;
453 stub.threadSafe = GL_FALSE;
454 stub.trackWindowSize = 0;
455 stub.trackWindowPos = 0;
456 stub.trackWindowVisibility = 0;
457 stub.trackWindowVisibleRgn = 0;
458 stub.mothershipPID = 0;
459 stub.spu_dir = NULL;
460
461 stub.freeContextNumber = MAGIC_CONTEXT_BASE;
462 stub.contextTable = crAllocHashtable();
463 stub.currentContext = NULL;
464
465 stub.windowTable = crAllocHashtable();
466
467#ifdef CR_NEWWINTRACK
468 stub.bShutdownSyncThread = false;
469 stub.hSyncThread = NIL_RTTHREAD;
470#endif
471
472 defaultWin = (WindowInfo *) crCalloc(sizeof(WindowInfo));
473 defaultWin->type = CHROMIUM;
474 defaultWin->spuWindow = 0; /* window 0 always exists */
475#ifdef WINDOWS
476 defaultWin->hVisibleRegion = INVALID_HANDLE_VALUE;
477#elif defined(GLX)
478 defaultWin->pVisibleRegions = NULL;
479 defaultWin->cVisibleRegions = 0;
480#endif
481 crHashtableAdd(stub.windowTable, 0, defaultWin);
482
483#if 1
484 atexit(stubExitHandler);
485 signal(SIGTERM, stubSignalHandler);
486 signal(SIGINT, stubSignalHandler);
487#ifndef WINDOWS
488 signal(SIGPIPE, SIG_IGN); /* the networking code should catch this */
489#endif
490#else
491 (void) stubExitHandler;
492 (void) stubSignalHandler;
493#endif
494}
495
496
497/**
498 * Return a free port number for the mothership to use, or -1 if we
499 * can't find one.
500 */
501static int
502GenerateMothershipPort(void)
503{
504 const int MAX_PORT = 10100;
505 unsigned short port;
506
507 /* generate initial port number randomly */
508 crRandAutoSeed();
509 port = (unsigned short) crRandInt(10001, MAX_PORT);
510
511#ifdef WINDOWS
512 /* XXX should implement a free port check here */
513 return port;
514#else
515 /*
516 * See if this port number really is free, try another if needed.
517 */
518 {
519 struct sockaddr_in servaddr;
520 int so_reuseaddr = 1;
521 int sock, k;
522
523 /* create socket */
524 sock = socket(AF_INET, SOCK_STREAM, 0);
525 CRASSERT(sock > 2);
526
527 /* deallocate socket/port when we exit */
528 k = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
529 (char *) &so_reuseaddr, sizeof(so_reuseaddr));
530 CRASSERT(k == 0);
531
532 /* initialize the servaddr struct */
533 crMemset(&servaddr, 0, sizeof(servaddr) );
534 servaddr.sin_family = AF_INET;
535 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
536
537 while (port < MAX_PORT) {
538 /* Bind to the given port number, return -1 if we fail */
539 servaddr.sin_port = htons((unsigned short) port);
540 k = bind(sock, (struct sockaddr *) &servaddr, sizeof(servaddr));
541 if (k) {
542 /* failed to create port. try next one. */
543 port++;
544 }
545 else {
546 /* free the socket/port now so mothership can make it */
547 close(sock);
548 return port;
549 }
550 }
551 }
552#endif /* WINDOWS */
553 return -1;
554}
555
556
557/**
558 * Try to determine which mothership configuration to use for this program.
559 */
560static char **
561LookupMothershipConfig(const char *procName)
562{
563 const int procNameLen = crStrlen(procName);
564 FILE *f;
565 const char *home;
566 char configPath[1000];
567
568 /* first, check if the CR_CONFIG env var is set */
569 {
570 const char *conf = crGetenv("CR_CONFIG");
571 if (conf && crStrlen(conf) > 0)
572 return crStrSplit(conf, " ");
573 }
574
575 /* second, look up config name from config file */
576 home = crGetenv("HOME");
577 if (home)
578 sprintf(configPath, "%s/%s", home, CONFIG_LOOKUP_FILE);
579 else
580 crStrcpy(configPath, CONFIG_LOOKUP_FILE); /* from current dir */
581 /* Check if the CR_CONFIG_PATH env var is set. */
582 {
583 const char *conf = crGetenv("CR_CONFIG_PATH");
584 if (conf)
585 crStrcpy(configPath, conf); /* from env var */
586 }
587
588 f = fopen(configPath, "r");
589 if (!f) {
590 return NULL;
591 }
592
593 while (!feof(f)) {
594 char line[1000];
595 char **args;
596 fgets(line, 999, f);
597 line[crStrlen(line) - 1] = 0; /* remove trailing newline */
598 if (crStrncmp(line, procName, procNameLen) == 0 &&
599 (line[procNameLen] == ' ' || line[procNameLen] == '\t'))
600 {
601 crWarning("Using Chromium configuration for %s from %s",
602 procName, configPath);
603 args = crStrSplit(line + procNameLen + 1, " ");
604 return args;
605 }
606 }
607 fclose(f);
608 return NULL;
609}
610
611
612static int Mothership_Awake = 0;
613
614
615/**
616 * Signal handler to determine when mothership is ready.
617 */
618static void
619MothershipPhoneHome(int signo)
620{
621 crDebug("Got signal %d: mothership is awake!", signo);
622 Mothership_Awake = 1;
623}
624
625void stubSetDefaultConfigurationOptions(void)
626{
627 unsigned char key[16]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
628
629 stub.appDrawCursor = 0;
630 stub.minChromiumWindowWidth = 0;
631 stub.minChromiumWindowHeight = 0;
632 stub.maxChromiumWindowWidth = 0;
633 stub.maxChromiumWindowHeight = 0;
634 stub.matchChromiumWindowID = NULL;
635 stub.numIgnoreWindowID = 0;
636 stub.matchWindowTitle = NULL;
637 stub.ignoreFreeglutMenus = 0;
638 stub.trackWindowSize = 1;
639 stub.trackWindowPos = 1;
640 stub.trackWindowVisibility = 1;
641 stub.trackWindowVisibleRgn = 1;
642 stub.matchChromiumWindowCount = 0;
643 stub.spu_dir = NULL;
644 crNetSetRank(0);
645 crNetSetContextRange(32, 35);
646 crNetSetNodeRange("iam0", "iamvis20");
647 crNetSetKey(key,sizeof(key));
648 stub.force_pbuffers = 0;
649 stub.viewportHack = 0;
650
651#ifdef WINDOWS
652 {
653 char name[1000];
654 int i;
655
656 /* Apply viewport hack only if we're running under wine */
657 if (NULL!=GetModuleHandle("wined3d.dll") || NULL != GetModuleHandle("wined3dwddm.dll"))
658 {
659 crGetProcName(name, 1000);
660 for (i=0; gsViewportHackApps[i]; ++i)
661 {
662 if (!stricmp(name, gsViewportHackApps[i]))
663 {
664 stub.viewportHack = 1;
665 break;
666 }
667 }
668 }
669 }
670#endif
671}
672
673#ifdef CR_NEWWINTRACK
674# ifdef VBOX_WITH_WDDM
675static stubDispatchVisibleRegions(WindowInfo *pWindow)
676{
677 DWORD dwCount;
678 LPRGNDATA lpRgnData;
679
680 dwCount = GetRegionData(pWindow->hVisibleRegion, 0, NULL);
681 lpRgnData = crAlloc(dwCount);
682
683 if (lpRgnData)
684 {
685 GetRegionData(pWindow->hVisibleRegion, dwCount, lpRgnData);
686 crDebug("Dispatched WindowVisibleRegion (%i, cRects=%i)", pWindow->spuWindow, lpRgnData->rdh.nCount);
687 stub.spuDispatch.WindowVisibleRegion(pWindow->spuWindow, lpRgnData->rdh.nCount, (GLint*) lpRgnData->Buffer);
688 crFree(lpRgnData);
689 }
690 else crWarning("GetRegionData failed, VisibleRegions update failed");
691}
692
693static HRGN stubMakeRegionFromRects(PVBOXVIDEOCM_CMD_RECTS pRegions, uint32_t start)
694{
695 HRGN hRgn, hTmpRgn;
696 uint32_t i;
697
698 if (pRegions->RectsInfo.cRects<=start)
699 {
700 return INVALID_HANDLE_VALUE;
701 }
702
703 hRgn = CreateRectRgn(0, 0, 0, 0);
704 for (i=start; i<pRegions->RectsInfo.cRects; ++i)
705 {
706 hTmpRgn = CreateRectRgnIndirect(&pRegions->RectsInfo.aRects[i]);
707 CombineRgn(hRgn, hRgn, hTmpRgn, RGN_OR);
708 DeleteObject(hTmpRgn);
709 }
710 return hRgn;
711}
712
713static void stubSyncTrUpdateWindowCB(unsigned long key, void *data1, void *data2)
714{
715 WindowInfo *pWindow = (WindowInfo *) data1;
716 VBOXDISPMP_REGIONS *pRegions = (VBOXDISPMP_REGIONS*) data2;
717 bool bChanged = false;
718 HRGN hNewRgn = INVALID_HANDLE_VALUE;
719
720 if (pRegions->hWnd != pWindow->hWnd)
721 {
722 return;
723 }
724
725 if (!pWindow->mapped)
726 {
727 pWindow->mapped = GL_TRUE;
728 bChanged = true;
729 crDebug("Dispatched: WindowShow(%i, %i)", pWindow->spuWindow, pWindow->mapped);
730 stub.spu->dispatch_table.WindowShow(pWindow->spuWindow, pWindow->mapped);
731 }
732
733 if (pRegions->pRegions->fFlags.bSetVisibleRects && pRegions->pRegions->fFlags.bSetViewRect)
734 {
735 int winX, winY;
736 unsigned int winW, winH;
737
738 winX = pRegions->pRegions->RectsInfo.aRects[0].left;
739 winY = pRegions->pRegions->RectsInfo.aRects[0].top;
740 winW = pRegions->pRegions->RectsInfo.aRects[0].right - winX;
741 winH = pRegions->pRegions->RectsInfo.aRects[0].bottom - winY;
742
743 if (stub.trackWindowPos && (winX!=pWindow->x || winY!=pWindow->y))
744 {
745 crDebug("Dispatched WindowPosition (%i)", pWindow->spuWindow);
746 stub.spuDispatch.WindowPosition(pWindow->spuWindow, winX, winY);
747 pWindow->x = winX;
748 pWindow->y = winY;
749 bChanged = true;
750 }
751
752 if (stub.trackWindowSize && (winW!=pWindow->width || winH!=pWindow->height))
753 {
754 crDebug("Dispatched WindowSize (%i)", pWindow->spuWindow);
755 stub.spuDispatch.WindowSize(pWindow->spuWindow, winW, winH);
756 pWindow->width = winW;
757 pWindow->height = winH;
758 bChanged = true;
759 }
760
761 hNewRgn = stubMakeRegionFromRects(pRegions->pRegions, 1);
762
763 /* ensure the window is in synch to avoid possible incorrect host notifications */
764 {
765 BOOL bRc = MoveWindow(pRegions->hWnd, winX, winY, winW, winH, FALSE /*BOOL bRepaint*/);
766 if (!bRc)
767 {
768 DWORD winEr = GetLastError();
769 crWarning("stubSyncTrUpdateWindowCB: MoveWindow failed winEr(%d)", winEr);
770 }
771 }
772 }
773 else
774 {
775 hNewRgn = stubMakeRegionFromRects(pRegions->pRegions, 0);
776 }
777
778 if (hNewRgn!=INVALID_HANDLE_VALUE)
779 {
780 OffsetRgn(hNewRgn, -pWindow->x, -pWindow->y);
781
782 if (pWindow->hVisibleRegion!=INVALID_HANDLE_VALUE)
783 {
784 CombineRgn(hNewRgn, pWindow->hVisibleRegion, hNewRgn,
785 pRegions->pRegions->fFlags.bAddHiddenRects ? RGN_DIFF:RGN_OR);
786
787 if (!EqualRgn(pWindow->hVisibleRegion, hNewRgn))
788 {
789 DeleteObject(pWindow->hVisibleRegion);
790 pWindow->hVisibleRegion = hNewRgn;
791 stubDispatchVisibleRegions(pWindow);
792 bChanged = true;
793 }
794 else
795 {
796 DeleteObject(hNewRgn);
797 }
798 }
799 else
800 {
801 if (pRegions->pRegions->fFlags.bSetVisibleRects)
802 {
803 pWindow->hVisibleRegion = hNewRgn;
804 stubDispatchVisibleRegions(pWindow);
805 bChanged = true;
806 }
807 }
808 }
809
810 if (bChanged)
811 {
812 stub.spu->dispatch_table.Flush();
813 }
814}
815# endif
816
817static void stubSyncTrCheckWindowsCB(unsigned long key, void *data1, void *data2)
818{
819 WindowInfo *pWindow = (WindowInfo *) data1;
820 (void) data2;
821
822 if (pWindow->type!=CHROMIUM || pWindow->spuWindow==0)
823 {
824 return;
825 }
826
827 stub.spu->dispatch_table.VBoxPackSetInjectID(pWindow->u32ClientID);
828
829 if (!stubSystemWindowExist(pWindow))
830 {
831#ifdef WINDOWS
832 crWindowDestroy((GLint)pWindow->hWnd);
833#else
834 crWindowDestroy((GLint)pWindow->drawable);
835#endif
836 /*No need to flush here as crWindowDestroy does it*/
837 return;
838 }
839
840 stubCheckWindowState(pWindow, GL_TRUE);
841}
842
843static DECLCALLBACK(int) stubSyncThreadProc(RTTHREAD ThreadSelf, void *pvUser)
844{
845#ifdef WINDOWS
846 MSG msg;
847# ifdef VBOX_WITH_WDDM
848 static VBOXDISPMP_CALLBACKS VBoxDispMpTstCallbacks = {NULL, NULL, NULL};
849 HMODULE hVBoxD3D = NULL;
850 VBOXDISPMP_REGIONS Regions;
851 HRESULT hr;
852# endif
853#endif
854
855 (void) pvUser;
856
857 crDebug("Sync thread started");
858#ifdef WINDOWS
859 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
860# ifdef VBOX_WITH_WDDM
861 if (GetModuleHandleEx(0, "VBoxDispD3D", &hVBoxD3D))
862 {
863 PFNVBOXDISPMP_GETCALLBACKS pfnVBoxDispMpGetCallbacks;
864 pfnVBoxDispMpGetCallbacks = (PFNVBOXDISPMP_GETCALLBACKS)GetProcAddress(hVBoxD3D, TEXT("VBoxDispMpGetCallbacks"));
865 if (pfnVBoxDispMpGetCallbacks)
866 {
867 hr = pfnVBoxDispMpGetCallbacks(VBOXDISPMP_VERSION, &VBoxDispMpTstCallbacks);
868 if (S_OK==hr)
869 {
870 CRASSERT(VBoxDispMpTstCallbacks.pfnEnableEvents);
871 CRASSERT(VBoxDispMpTstCallbacks.pfnDisableEvents);
872 CRASSERT(VBoxDispMpTstCallbacks.pfnGetRegions);
873
874 hr = VBoxDispMpTstCallbacks.pfnEnableEvents();
875 if (hr != S_OK)
876 {
877 crWarning("VBoxDispMpTstCallbacks.pfnEnableEvents failed");
878 }
879 else
880 {
881 crDebug("running with VBoxDispD3D");
882 }
883 }
884 else
885 {
886 crWarning("VBoxDispMpGetCallbacks failed");
887 }
888 }
889 }
890# endif
891#endif
892
893 crLockMutex(&stub.mutex);
894 stub.spu->dispatch_table.VBoxPackSetInjectThread();
895 crUnlockMutex(&stub.mutex);
896
897 RTThreadUserSignal(ThreadSelf);
898
899 while(!stub.bShutdownSyncThread)
900 {
901#ifdef WINDOWS
902 if (!PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
903 {
904# ifdef VBOX_WITH_WDDM
905 if (VBoxDispMpTstCallbacks.pfnGetRegions)
906 {
907 hr = VBoxDispMpTstCallbacks.pfnGetRegions(&Regions, 50);
908 if (S_OK==hr)
909 {
910# if 0
911 uint32_t i;
912 crDebug(">>>Regions for HWND(0x%x)>>>", Regions.hWnd);
913 crDebug("Flags(0x%x)", Regions.pRegions->fFlags.Value);
914 for (i = 0; i < Regions.pRegions->RectsInfo.cRects; ++i)
915 {
916 RECT *pRect = &Regions.pRegions->RectsInfo.aRects[i];
917 crDebug("Rect(%d): left(%d), top(%d), right(%d), bottom(%d)", i, pRect->left, pRect->top, pRect->right, pRect->bottom);
918 }
919 crDebug("<<<<<");
920# endif
921 /*hacky way to make sure window wouldn't be deleted in another thread as we hold hashtable lock here*/
922 crHashtableWalk(stub.windowTable, stubSyncTrUpdateWindowCB, &Regions);
923 }
924 else
925 {
926 if (WAIT_TIMEOUT!=hr)
927 {
928 crWarning("VBoxDispMpTstCallbacks.pfnGetRegions failed with 0x%x", hr);
929 }
930 }
931 }
932 else
933# endif
934 {
935 crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
936 RTThreadSleep(50);
937 }
938 }
939 else
940 {
941 if (WM_QUIT==msg.message)
942 {
943 crDebug("Sync thread got WM_QUIT");
944 break;
945 }
946 else
947 {
948 TranslateMessage(&msg);
949 DispatchMessage(&msg);
950 }
951 }
952#else
953 crLockMutex(&stub.mutex);
954 crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
955 crUnlockMutex(&stub.mutex);
956 RTThreadSleep(50);
957#endif
958 }
959
960#ifdef VBOX_WITH_WDDM
961 if (VBoxDispMpTstCallbacks.pfnDisableEvents)
962 {
963 VBoxDispMpTstCallbacks.pfnDisableEvents();
964 }
965 if (hVBoxD3D)
966 {
967 FreeLibrary(hVBoxD3D);
968 }
969#endif
970 crDebug("Sync thread stopped");
971 return 0;
972}
973#endif
974
975/**
976 * Do one-time initializations for the faker.
977 * Returns TRUE on success, FALSE otherwise.
978 */
979bool
980stubInit(void)
981{
982 /* Here is where we contact the mothership to find out what we're supposed
983 * to be doing. Networking code in a DLL initializer. I sure hope this
984 * works :)
985 *
986 * HOW can I pass the mothership address to this if I already know it?
987 */
988
989 CRConnection *conn = NULL;
990 char response[1024];
991 char **spuchain;
992 int num_spus;
993 int *spu_ids;
994 char **spu_names;
995 const char *app_id;
996 int i;
997 int disable_sync = 0;
998
999 if (stub_initialized)
1000 return true;
1001
1002 stubInitVars();
1003
1004 crGetProcName(response, 1024);
1005 crDebug("Stub launched for %s", response);
1006
1007#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
1008 /*@todo when vm boots with compiz turned on, new code causes hang in xcb_wait_for_reply in the sync thread*/
1009 if (!crStrcmp(response, "compiz") || !crStrcmp(response, "compiz_real")
1010 || !crStrcmp(response, "compiz-bin"))
1011 {
1012 disable_sync = 1;
1013 }
1014#endif
1015
1016 /* @todo check if it'd be of any use on other than guests, no use for windows */
1017 app_id = crGetenv( "CR_APPLICATION_ID_NUMBER" );
1018
1019 crNetInit( NULL, NULL );
1020
1021#ifndef WINDOWS
1022 {
1023 CRNetServer ns;
1024
1025 ns.name = "vboxhgcm://host:0";
1026 ns.buffer_size = 1024;
1027 crNetServerConnect(&ns);
1028 if (!ns.conn)
1029 {
1030 crWarning("Failed to connect to host. Make sure 3D acceleration is enabled for this VM.");
1031 return false;
1032 }
1033 else
1034 {
1035 crNetFreeConnection(ns.conn);
1036 }
1037#if 0 && defined(CR_NEWWINTRACK)
1038 {
1039 Status st = XInitThreads();
1040 if (st==0)
1041 {
1042 crWarning("XInitThreads returned %i", (int)st);
1043 }
1044 }
1045#endif
1046 }
1047#endif
1048
1049 strcpy(response, "2 0 feedback 1 pack");
1050 spuchain = crStrSplit( response, " " );
1051 num_spus = crStrToInt( spuchain[0] );
1052 spu_ids = (int *) crAlloc( num_spus * sizeof( *spu_ids ) );
1053 spu_names = (char **) crAlloc( num_spus * sizeof( *spu_names ) );
1054 for (i = 0 ; i < num_spus ; i++)
1055 {
1056 spu_ids[i] = crStrToInt( spuchain[2*i+1] );
1057 spu_names[i] = crStrdup( spuchain[2*i+2] );
1058 crDebug( "SPU %d/%d: (%d) \"%s\"", i+1, num_spus, spu_ids[i], spu_names[i] );
1059 }
1060
1061 stubSetDefaultConfigurationOptions();
1062
1063 stub.spu = crSPULoadChain( num_spus, spu_ids, spu_names, stub.spu_dir, NULL );
1064
1065 crFree( spuchain );
1066 crFree( spu_ids );
1067 for (i = 0; i < num_spus; ++i)
1068 crFree(spu_names[i]);
1069 crFree( spu_names );
1070
1071 // spu chain load failed somewhere
1072 if (!stub.spu) {
1073 return false;
1074 }
1075
1076 crSPUInitDispatchTable( &glim );
1077
1078 /* This is unlikely to change -- We still want to initialize our dispatch
1079 * table with the functions of the first SPU in the chain. */
1080 stubInitSPUDispatch( stub.spu );
1081
1082 /* we need to plug one special stub function into the dispatch table */
1083 glim.GetChromiumParametervCR = stub_GetChromiumParametervCR;
1084
1085#if !defined(VBOX_NO_NATIVEGL)
1086 /* Load pointers to native OpenGL functions into stub.nativeDispatch */
1087 stubInitNativeDispatch();
1088#endif
1089
1090/*crDebug("stub init");
1091raise(SIGINT);*/
1092
1093#ifdef WINDOWS
1094# ifndef CR_NEWWINTRACK
1095 stubInstallWindowMessageHook();
1096# endif
1097#endif
1098
1099#ifdef CR_NEWWINTRACK
1100 {
1101 int rc;
1102
1103 RTR3Init();
1104
1105 if (!disable_sync)
1106 {
1107 crDebug("Starting sync thread");
1108
1109 rc = RTThreadCreate(&stub.hSyncThread, stubSyncThreadProc, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "Sync");
1110 if (RT_FAILURE(rc))
1111 {
1112 crError("Failed to start sync thread! (%x)", rc);
1113 }
1114 RTThreadUserWait(stub.hSyncThread, 60 * 1000);
1115 RTThreadUserReset(stub.hSyncThread);
1116
1117 crDebug("Going on");
1118 }
1119 }
1120#endif
1121
1122#ifdef GLX
1123 stub.xshmSI.shmid = -1;
1124 stub.bShmInitFailed = GL_FALSE;
1125 stub.pGLXPixmapsHash = crAllocHashtable();
1126#endif
1127
1128 stub_initialized = 1;
1129 return true;
1130}
1131
1132/* Sigh -- we can't do initialization at load time, since Windows forbids
1133 * the loading of other libraries from DLLMain. */
1134
1135#ifdef LINUX
1136/* GCC crap
1137 *void (*stub_init_ptr)(void) __attribute__((section(".ctors"))) = __stubInit; */
1138#endif
1139
1140#ifdef WINDOWS
1141#define WIN32_LEAN_AND_MEAN
1142#include <windows.h>
1143
1144/* Windows crap */
1145BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
1146{
1147 (void) lpvReserved;
1148
1149 switch (fdwReason)
1150 {
1151 case DLL_PROCESS_ATTACH:
1152 {
1153 CRNetServer ns;
1154
1155 crNetInit(NULL, NULL);
1156 ns.name = "vboxhgcm://host:0";
1157 ns.buffer_size = 1024;
1158 crNetServerConnect(&ns);
1159 if (!ns.conn)
1160 {
1161 crDebug("Failed to connect to host (is guest 3d acceleration enabled?), aborting ICD load.");
1162 return FALSE;
1163 }
1164 else
1165 crNetFreeConnection(ns.conn);
1166
1167 break;
1168 }
1169
1170 case DLL_PROCESS_DETACH:
1171 {
1172 stubSPUSafeTearDown();
1173 break;
1174 }
1175
1176 case DLL_THREAD_ATTACH:
1177 break;
1178
1179 case DLL_THREAD_DETACH:
1180 break;
1181
1182 default:
1183 break;
1184 }
1185
1186 return TRUE;
1187}
1188#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette