VirtualBox

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

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

WDDM/3d: force desktop repaint after visibile region changes changes

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 34.9 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/Video/common/wddm/VBoxMPIf.h"
37#include "../../WINNT/Graphics/Video/disp/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 (pWindow->hWnd!=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(WINDOWS) && defined(VBOX_WITH_WDDM)
201 if (stub.bRunningUnderWDDM)
202 return;
203#endif
204
205#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
206 crLockMutex(&stub.mutex);
207#endif
208
209 stubCheckWindowState(stub.currentContext->currentDrawable, GL_TRUE);
210 crHashtableWalk(stub.windowTable, stubCheckWindowsCB, stub.currentContext);
211
212#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
213 crUnlockMutex(&stub.mutex);
214#endif
215}
216
217
218/**
219 * Override the head SPU's glClear function.
220 * We're basically trapping this function so that we can poll the
221 * application window size at a regular interval.
222 */
223static void SPU_APIENTRY trapClear(GLbitfield mask)
224{
225 stubCheckWindowsState();
226 /* call the original SPU glClear function */
227 origClear(mask);
228}
229
230/**
231 * As above, but for glViewport. Most apps call glViewport before
232 * glClear when a window is resized.
233 */
234static void SPU_APIENTRY trapViewport(GLint x, GLint y, GLsizei w, GLsizei h)
235{
236 stubCheckWindowsState();
237 /* call the original SPU glViewport function */
238 if (!stub.viewportHack)
239 {
240 origViewport(x, y, w, h);
241 }
242 else
243 {
244 int winX, winY;
245 unsigned int winW, winH;
246 WindowInfo *pWindow;
247 pWindow = stub.currentContext->currentDrawable;
248 stubGetWindowGeometry(pWindow, &winX, &winY, &winW, &winH);
249 origViewport(0, 0, winW, winH);
250 }
251}
252
253static void SPU_APIENTRY trapSwapBuffers(GLint window, GLint flags)
254{
255 stubCheckWindowsState();
256 origSwapBuffers(window, flags);
257}
258
259static void SPU_APIENTRY trapDrawBuffer(GLenum buf)
260{
261 stubCheckWindowsState();
262 origDrawBuffer(buf);
263}
264
265static void SPU_APIENTRY trapScissor(GLint x, GLint y, GLsizei w, GLsizei h)
266{
267 int winX, winY;
268 unsigned int winW, winH;
269 WindowInfo *pWindow;
270 pWindow = stub.currentContext->currentDrawable;
271 stubGetWindowGeometry(pWindow, &winX, &winY, &winW, &winH);
272 origScissor(0, 0, winW, winH);
273}
274
275/**
276 * Use the GL function pointers in <spu> to initialize the static glim
277 * dispatch table.
278 */
279static void stubInitSPUDispatch(SPU *spu)
280{
281 crSPUInitDispatchTable( &stub.spuDispatch );
282 crSPUCopyDispatchTable( &stub.spuDispatch, &(spu->dispatch_table) );
283
284 if (stub.trackWindowSize || stub.trackWindowPos || stub.trackWindowVisibleRgn) {
285 /* patch-in special glClear/Viewport function to track window sizing */
286 origClear = stub.spuDispatch.Clear;
287 origViewport = stub.spuDispatch.Viewport;
288 origSwapBuffers = stub.spuDispatch.SwapBuffers;
289 origDrawBuffer = stub.spuDispatch.DrawBuffer;
290 origScissor = stub.spuDispatch.Scissor;
291 stub.spuDispatch.Clear = trapClear;
292 stub.spuDispatch.Viewport = trapViewport;
293
294 if (stub.viewportHack)
295 stub.spuDispatch.Scissor = trapScissor;
296 /*stub.spuDispatch.SwapBuffers = trapSwapBuffers;
297 stub.spuDispatch.DrawBuffer = trapDrawBuffer;*/
298 }
299
300 crSPUCopyDispatchTable( &glim, &stub.spuDispatch );
301}
302
303// Callback function, used to destroy all created contexts
304static void hsWalkStubDestroyContexts(unsigned long key, void *data1, void *data2)
305{
306 stubDestroyContext(key);
307}
308
309/**
310 * This is called when we exit.
311 * We call all the SPU's cleanup functions.
312 */
313static void stubSPUTearDown(void)
314{
315 crDebug("stubSPUTearDown");
316 if (!stub_initialized) return;
317
318 stub_initialized = 0;
319
320#ifdef WINDOWS
321# ifndef CR_NEWWINTRACK
322 stubUninstallWindowMessageHook();
323# endif
324#endif
325
326#ifdef CR_NEWWINTRACK
327 ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
328#endif
329
330 //delete all created contexts
331 stubMakeCurrent( NULL, NULL);
332 crHashtableWalk(stub.contextTable, hsWalkStubDestroyContexts, NULL);
333
334 /* shutdown, now trap any calls to a NULL dispatcher */
335 crSPUCopyDispatchTable(&glim, &stubNULLDispatch);
336
337 crSPUUnloadChain(stub.spu);
338 stub.spu = NULL;
339
340#ifndef Linux
341 crUnloadOpenGL();
342#endif
343
344#ifndef WINDOWS
345 crNetTearDown();
346#endif
347
348#ifdef GLX
349 if (stub.xshmSI.shmid>=0)
350 {
351 shmctl(stub.xshmSI.shmid, IPC_RMID, 0);
352 shmdt(stub.xshmSI.shmaddr);
353 }
354 crFreeHashtable(stub.pGLXPixmapsHash, crFree);
355#endif
356
357 crFreeHashtable(stub.windowTable, crFree);
358 crFreeHashtable(stub.contextTable, NULL);
359
360 crMemset(&stub, 0, sizeof(stub));
361}
362
363static void stubSPUSafeTearDown(void)
364{
365#ifdef CHROMIUM_THREADSAFE
366 CRmutex *mutex;
367#endif
368
369 if (!stub_initialized) return;
370 stub_initialized = 0;
371
372#ifdef CHROMIUM_THREADSAFE
373 mutex = &stub.mutex;
374 crLockMutex(mutex);
375#endif
376 crDebug("stubSPUSafeTearDown");
377
378#ifdef WINDOWS
379# ifndef CR_NEWWINTRACK
380 stubUninstallWindowMessageHook();
381# endif
382#endif
383
384#if defined(CR_NEWWINTRACK)
385 crUnlockMutex(mutex);
386# if defined(WINDOWS)
387 if (RTThreadGetState(stub.hSyncThread)!=RTTHREADSTATE_TERMINATED)
388 {
389 HANDLE hNative;
390 DWORD ec=0;
391
392 hNative = OpenThread(SYNCHRONIZE|THREAD_QUERY_INFORMATION|THREAD_TERMINATE,
393 false, RTThreadGetNative(stub.hSyncThread));
394 if (!hNative)
395 {
396 crWarning("Failed to get handle for sync thread(%#x)", GetLastError());
397 }
398 else
399 {
400 crDebug("Got handle %p for thread %#x", hNative, RTThreadGetNative(stub.hSyncThread));
401 }
402
403 ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
404
405 if (PostThreadMessage(RTThreadGetNative(stub.hSyncThread), WM_QUIT, 0, 0))
406 {
407 RTThreadWait(stub.hSyncThread, 1000, NULL);
408
409 /*Same issue as on linux, RTThreadWait exits before system thread is terminated, which leads
410 * to issues as our dll goes to be unloaded.
411 *@todo
412 *We usually call this function from DllMain which seems to be holding some lock and thus we have to
413 * kill thread via TerminateThread.
414 */
415 if (WaitForSingleObject(hNative, 100)==WAIT_TIMEOUT)
416 {
417 crDebug("Wait failed, terminating");
418 if (!TerminateThread(hNative, 1))
419 {
420 crDebug("TerminateThread failed");
421 }
422 }
423 if (GetExitCodeThread(hNative, &ec))
424 {
425 crDebug("Thread %p exited with ec=%i", hNative, ec);
426 }
427 else
428 {
429 crDebug("GetExitCodeThread failed(%#x)", GetLastError());
430 }
431 }
432 else
433 {
434 crDebug("Sync thread killed before DLL_PROCESS_DETACH");
435 }
436
437 if (hNative)
438 {
439 CloseHandle(hNative);
440 }
441 }
442#else
443 if (stub.hSyncThread!=NIL_RTTHREAD)
444 {
445 ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
446 {
447 /*RTThreadWait might return too early, which cause our code being unloaded while RT thread wrapper is still running*/
448 int rc = pthread_join(RTThreadGetNative(stub.hSyncThread), NULL);
449 if (!rc)
450 {
451 crDebug("pthread_join failed %i", rc);
452 }
453 }
454 }
455#endif
456 crLockMutex(mutex);
457#endif
458
459#ifndef WINDOWS
460 crNetTearDown();
461#endif
462
463#ifdef CHROMIUM_THREADSAFE
464 crUnlockMutex(mutex);
465 crFreeMutex(mutex);
466#endif
467 crMemset(&stub, 0, sizeof(stub));
468}
469
470
471static void stubExitHandler(void)
472{
473 stubSPUSafeTearDown();
474}
475
476/**
477 * Called when we receive a SIGTERM signal.
478 */
479static void stubSignalHandler(int signo)
480{
481 stubSPUSafeTearDown();
482 exit(0); /* this causes stubExitHandler() to be called */
483}
484
485
486/**
487 * Init variables in the stub structure, install signal handler.
488 */
489static void stubInitVars(void)
490{
491 WindowInfo *defaultWin;
492
493#ifdef CHROMIUM_THREADSAFE
494 crInitMutex(&stub.mutex);
495#endif
496
497 /* At the very least we want CR_RGB_BIT. */
498 stub.haveNativeOpenGL = GL_FALSE;
499 stub.spu = NULL;
500 stub.appDrawCursor = 0;
501 stub.minChromiumWindowWidth = 0;
502 stub.minChromiumWindowHeight = 0;
503 stub.maxChromiumWindowWidth = 0;
504 stub.maxChromiumWindowHeight = 0;
505 stub.matchChromiumWindowCount = 0;
506 stub.matchChromiumWindowID = NULL;
507 stub.matchWindowTitle = NULL;
508 stub.ignoreFreeglutMenus = 0;
509 stub.threadSafe = GL_FALSE;
510 stub.trackWindowSize = 0;
511 stub.trackWindowPos = 0;
512 stub.trackWindowVisibility = 0;
513 stub.trackWindowVisibleRgn = 0;
514 stub.mothershipPID = 0;
515 stub.spu_dir = NULL;
516
517 stub.freeContextNumber = MAGIC_CONTEXT_BASE;
518 stub.contextTable = crAllocHashtable();
519 stub.currentContext = NULL;
520
521 stub.windowTable = crAllocHashtable();
522
523#ifdef CR_NEWWINTRACK
524 stub.bShutdownSyncThread = false;
525 stub.hSyncThread = NIL_RTTHREAD;
526#endif
527
528 defaultWin = (WindowInfo *) crCalloc(sizeof(WindowInfo));
529 defaultWin->type = CHROMIUM;
530 defaultWin->spuWindow = 0; /* window 0 always exists */
531#ifdef WINDOWS
532 defaultWin->hVisibleRegion = INVALID_HANDLE_VALUE;
533#elif defined(GLX)
534 defaultWin->pVisibleRegions = NULL;
535 defaultWin->cVisibleRegions = 0;
536#endif
537 crHashtableAdd(stub.windowTable, 0, defaultWin);
538
539#if 1
540 atexit(stubExitHandler);
541 signal(SIGTERM, stubSignalHandler);
542 signal(SIGINT, stubSignalHandler);
543#ifndef WINDOWS
544 signal(SIGPIPE, SIG_IGN); /* the networking code should catch this */
545#endif
546#else
547 (void) stubExitHandler;
548 (void) stubSignalHandler;
549#endif
550}
551
552
553/**
554 * Return a free port number for the mothership to use, or -1 if we
555 * can't find one.
556 */
557static int
558GenerateMothershipPort(void)
559{
560 const int MAX_PORT = 10100;
561 unsigned short port;
562
563 /* generate initial port number randomly */
564 crRandAutoSeed();
565 port = (unsigned short) crRandInt(10001, MAX_PORT);
566
567#ifdef WINDOWS
568 /* XXX should implement a free port check here */
569 return port;
570#else
571 /*
572 * See if this port number really is free, try another if needed.
573 */
574 {
575 struct sockaddr_in servaddr;
576 int so_reuseaddr = 1;
577 int sock, k;
578
579 /* create socket */
580 sock = socket(AF_INET, SOCK_STREAM, 0);
581 CRASSERT(sock > 2);
582
583 /* deallocate socket/port when we exit */
584 k = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
585 (char *) &so_reuseaddr, sizeof(so_reuseaddr));
586 CRASSERT(k == 0);
587
588 /* initialize the servaddr struct */
589 crMemset(&servaddr, 0, sizeof(servaddr) );
590 servaddr.sin_family = AF_INET;
591 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
592
593 while (port < MAX_PORT) {
594 /* Bind to the given port number, return -1 if we fail */
595 servaddr.sin_port = htons((unsigned short) port);
596 k = bind(sock, (struct sockaddr *) &servaddr, sizeof(servaddr));
597 if (k) {
598 /* failed to create port. try next one. */
599 port++;
600 }
601 else {
602 /* free the socket/port now so mothership can make it */
603 close(sock);
604 return port;
605 }
606 }
607 }
608#endif /* WINDOWS */
609 return -1;
610}
611
612
613/**
614 * Try to determine which mothership configuration to use for this program.
615 */
616static char **
617LookupMothershipConfig(const char *procName)
618{
619 const int procNameLen = crStrlen(procName);
620 FILE *f;
621 const char *home;
622 char configPath[1000];
623
624 /* first, check if the CR_CONFIG env var is set */
625 {
626 const char *conf = crGetenv("CR_CONFIG");
627 if (conf && crStrlen(conf) > 0)
628 return crStrSplit(conf, " ");
629 }
630
631 /* second, look up config name from config file */
632 home = crGetenv("HOME");
633 if (home)
634 sprintf(configPath, "%s/%s", home, CONFIG_LOOKUP_FILE);
635 else
636 crStrcpy(configPath, CONFIG_LOOKUP_FILE); /* from current dir */
637 /* Check if the CR_CONFIG_PATH env var is set. */
638 {
639 const char *conf = crGetenv("CR_CONFIG_PATH");
640 if (conf)
641 crStrcpy(configPath, conf); /* from env var */
642 }
643
644 f = fopen(configPath, "r");
645 if (!f) {
646 return NULL;
647 }
648
649 while (!feof(f)) {
650 char line[1000];
651 char **args;
652 fgets(line, 999, f);
653 line[crStrlen(line) - 1] = 0; /* remove trailing newline */
654 if (crStrncmp(line, procName, procNameLen) == 0 &&
655 (line[procNameLen] == ' ' || line[procNameLen] == '\t'))
656 {
657 crWarning("Using Chromium configuration for %s from %s",
658 procName, configPath);
659 args = crStrSplit(line + procNameLen + 1, " ");
660 return args;
661 }
662 }
663 fclose(f);
664 return NULL;
665}
666
667
668static int Mothership_Awake = 0;
669
670
671/**
672 * Signal handler to determine when mothership is ready.
673 */
674static void
675MothershipPhoneHome(int signo)
676{
677 crDebug("Got signal %d: mothership is awake!", signo);
678 Mothership_Awake = 1;
679}
680
681void stubSetDefaultConfigurationOptions(void)
682{
683 unsigned char key[16]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
684
685 stub.appDrawCursor = 0;
686 stub.minChromiumWindowWidth = 0;
687 stub.minChromiumWindowHeight = 0;
688 stub.maxChromiumWindowWidth = 0;
689 stub.maxChromiumWindowHeight = 0;
690 stub.matchChromiumWindowID = NULL;
691 stub.numIgnoreWindowID = 0;
692 stub.matchWindowTitle = NULL;
693 stub.ignoreFreeglutMenus = 0;
694 stub.trackWindowSize = 1;
695 stub.trackWindowPos = 1;
696 stub.trackWindowVisibility = 1;
697 stub.trackWindowVisibleRgn = 1;
698 stub.matchChromiumWindowCount = 0;
699 stub.spu_dir = NULL;
700 crNetSetRank(0);
701 crNetSetContextRange(32, 35);
702 crNetSetNodeRange("iam0", "iamvis20");
703 crNetSetKey(key,sizeof(key));
704 stub.force_pbuffers = 0;
705 stub.viewportHack = 0;
706
707#ifdef WINDOWS
708 {
709 char name[1000];
710 int i;
711
712# ifdef VBOX_WITH_WDDM
713 stub.bRunningUnderWDDM = false;
714# endif
715 /* Apply viewport hack only if we're running under wine */
716 if (NULL!=GetModuleHandle("wined3d.dll") || NULL != GetModuleHandle("wined3dwddm.dll"))
717 {
718 crGetProcName(name, 1000);
719 for (i=0; gsViewportHackApps[i]; ++i)
720 {
721 if (!stricmp(name, gsViewportHackApps[i]))
722 {
723 stub.viewportHack = 1;
724 break;
725 }
726 }
727 }
728 }
729#endif
730}
731
732#ifdef CR_NEWWINTRACK
733# ifdef VBOX_WITH_WDDM
734static stubDispatchVisibleRegions(WindowInfo *pWindow)
735{
736 DWORD dwCount;
737 LPRGNDATA lpRgnData;
738
739 dwCount = GetRegionData(pWindow->hVisibleRegion, 0, NULL);
740 lpRgnData = crAlloc(dwCount);
741
742 if (lpRgnData)
743 {
744 GetRegionData(pWindow->hVisibleRegion, dwCount, lpRgnData);
745 crDebug("Dispatched WindowVisibleRegion (%i, cRects=%i)", pWindow->spuWindow, lpRgnData->rdh.nCount);
746 stub.spuDispatch.WindowVisibleRegion(pWindow->spuWindow, lpRgnData->rdh.nCount, (GLint*) lpRgnData->Buffer);
747 crFree(lpRgnData);
748 }
749 else crWarning("GetRegionData failed, VisibleRegions update failed");
750}
751
752static HRGN stubMakeRegionFromRects(PVBOXVIDEOCM_CMD_RECTS pRegions, uint32_t start)
753{
754 HRGN hRgn, hTmpRgn;
755 uint32_t i;
756
757 if (pRegions->RectsInfo.cRects<=start)
758 {
759 return INVALID_HANDLE_VALUE;
760 }
761
762 hRgn = CreateRectRgn(0, 0, 0, 0);
763 for (i=start; i<pRegions->RectsInfo.cRects; ++i)
764 {
765 hTmpRgn = CreateRectRgnIndirect(&pRegions->RectsInfo.aRects[i]);
766 CombineRgn(hRgn, hRgn, hTmpRgn, RGN_OR);
767 DeleteObject(hTmpRgn);
768 }
769 return hRgn;
770}
771
772static void stubSyncTrUpdateWindowCB(unsigned long key, void *data1, void *data2)
773{
774 WindowInfo *pWindow = (WindowInfo *) data1;
775 VBOXDISPMP_REGIONS *pRegions = (VBOXDISPMP_REGIONS*) data2;
776 bool bChanged = false;
777 HRGN hNewRgn = INVALID_HANDLE_VALUE;
778
779 if (pRegions->hWnd != pWindow->hWnd)
780 {
781 return;
782 }
783
784 stub.spu->dispatch_table.VBoxPackSetInjectID(pWindow->u32ClientID);
785
786 if (!stubSystemWindowExist(pWindow))
787 {
788 crWindowDestroy((GLint)pWindow->hWnd);
789 return;
790 }
791
792 if (!pWindow->mapped)
793 {
794 pWindow->mapped = GL_TRUE;
795 bChanged = true;
796 crDebug("Dispatched: WindowShow(%i, %i)", pWindow->spuWindow, pWindow->mapped);
797 stub.spu->dispatch_table.WindowShow(pWindow->spuWindow, pWindow->mapped);
798 }
799
800 if (pRegions->pRegions->fFlags.bSetVisibleRects || pRegions->pRegions->fFlags.bSetViewRect)
801 {
802 /* ensure data integrity */
803 Assert(!pRegions->pRegions->fFlags.bAddHiddenRects);
804
805 if (pRegions->pRegions->fFlags.bSetViewRect)
806 {
807 int winX, winY;
808 unsigned int winW, winH;
809 BOOL bRc;
810
811 winX = pRegions->pRegions->RectsInfo.aRects[0].left;
812 winY = pRegions->pRegions->RectsInfo.aRects[0].top;
813 winW = pRegions->pRegions->RectsInfo.aRects[0].right - winX;
814 winH = pRegions->pRegions->RectsInfo.aRects[0].bottom - winY;
815
816 if (stub.trackWindowPos && (winX!=pWindow->x || winY!=pWindow->y))
817 {
818 crDebug("Dispatched WindowPosition (%i)", pWindow->spuWindow);
819 stub.spuDispatch.WindowPosition(pWindow->spuWindow, winX, winY);
820 pWindow->x = winX;
821 pWindow->y = winY;
822 bChanged = true;
823 }
824
825 if (stub.trackWindowSize && (winW!=pWindow->width || winH!=pWindow->height))
826 {
827 crDebug("Dispatched WindowSize (%i)", pWindow->spuWindow);
828 stub.spuDispatch.WindowSize(pWindow->spuWindow, winW, winH);
829 pWindow->width = winW;
830 pWindow->height = winH;
831 bChanged = true;
832 }
833
834 bRc = MoveWindow(pRegions->hWnd, winX, winY, winW, winH, FALSE /*BOOL bRepaint*/);
835 if (!bRc)
836 {
837 DWORD winEr = GetLastError();
838 crWarning("stubSyncTrUpdateWindowCB: MoveWindow failed winEr(%d)", winEr);
839 }
840 }
841
842 if (pRegions->pRegions->fFlags.bSetVisibleRects)
843 {
844 hNewRgn = stubMakeRegionFromRects(pRegions->pRegions, pRegions->pRegions->fFlags.bSetViewRect ? 1 : 0);
845 }
846 }
847 else if (!pRegions->pRegions->fFlags.bHide)
848 {
849 Assert(pRegions->pRegions->fFlags.bAddHiddenRects);
850 hNewRgn = stubMakeRegionFromRects(pRegions->pRegions, 0);
851 }
852 else
853 {
854 Assert(pRegions->pRegions->fFlags.bAddHiddenRects);
855 hNewRgn = CreateRectRgn(pWindow->x, pWindow->y, pWindow->x + pWindow->width, pWindow->y + pWindow->height);
856 }
857
858 if (hNewRgn!=INVALID_HANDLE_VALUE)
859 {
860 if (pRegions->pRegions->fFlags.bSetVisibleRects)
861 {
862 HRGN hEmptyRgn = CreateRectRgn(0, 0, 0, 0);
863
864 if (hEmptyRgn!=INVALID_HANDLE_VALUE)
865 {
866 if (pWindow->hVisibleRegion==INVALID_HANDLE_VALUE || EqualRgn(pWindow->hVisibleRegion, hEmptyRgn))
867 {
868 SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0, SMTO_NORMAL, 1000, NULL);
869 }
870
871 DeleteObject(hEmptyRgn);
872 }
873 else
874 {
875 crWarning("Failed to created empty region!");
876 }
877 }
878
879 OffsetRgn(hNewRgn, -pWindow->x, -pWindow->y);
880
881 if (pWindow->hVisibleRegion!=INVALID_HANDLE_VALUE)
882 {
883 CombineRgn(hNewRgn, pWindow->hVisibleRegion, hNewRgn,
884 pRegions->pRegions->fFlags.bAddHiddenRects ? RGN_DIFF:RGN_OR);
885
886 if (!EqualRgn(pWindow->hVisibleRegion, hNewRgn))
887 {
888 DeleteObject(pWindow->hVisibleRegion);
889 pWindow->hVisibleRegion = hNewRgn;
890 stubDispatchVisibleRegions(pWindow);
891 bChanged = true;
892 }
893 else
894 {
895 DeleteObject(hNewRgn);
896 }
897 }
898 else
899 {
900 if (pRegions->pRegions->fFlags.bSetVisibleRects)
901 {
902 pWindow->hVisibleRegion = hNewRgn;
903 stubDispatchVisibleRegions(pWindow);
904 bChanged = true;
905 }
906 }
907 }
908
909 if (bChanged)
910 {
911 stub.spu->dispatch_table.Flush();
912 }
913}
914# endif
915
916static void stubSyncTrCheckWindowsCB(unsigned long key, void *data1, void *data2)
917{
918 WindowInfo *pWindow = (WindowInfo *) data1;
919 (void) data2;
920
921 if (pWindow->type!=CHROMIUM || pWindow->spuWindow==0)
922 {
923 return;
924 }
925
926 stub.spu->dispatch_table.VBoxPackSetInjectID(pWindow->u32ClientID);
927
928 if (!stubSystemWindowExist(pWindow))
929 {
930#ifdef WINDOWS
931 crWindowDestroy((GLint)pWindow->hWnd);
932#else
933 crWindowDestroy((GLint)pWindow->drawable);
934#endif
935 /*No need to flush here as crWindowDestroy does it*/
936 return;
937 }
938
939#if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
940 if (stub.bRunningUnderWDDM)
941 return;
942#endif
943 stubCheckWindowState(pWindow, GL_TRUE);
944}
945
946static DECLCALLBACK(int) stubSyncThreadProc(RTTHREAD ThreadSelf, void *pvUser)
947{
948#ifdef WINDOWS
949 MSG msg;
950# ifdef VBOX_WITH_WDDM
951 static VBOXDISPMP_CALLBACKS VBoxDispMpTstCallbacks = {NULL, NULL, NULL};
952 HMODULE hVBoxD3D = NULL;
953 VBOXDISPMP_REGIONS Regions;
954 HRESULT hr;
955# endif
956#endif
957
958 (void) pvUser;
959
960 crDebug("Sync thread started");
961#ifdef WINDOWS
962 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
963# ifdef VBOX_WITH_WDDM
964 hVBoxD3D = GetModuleHandle("VBoxDispD3D");
965 if (hVBoxD3D)
966 {
967 hVBoxD3D = LoadLibrary("VBoxDispD3D");
968 }
969
970 if (hVBoxD3D)
971 {
972 PFNVBOXDISPMP_GETCALLBACKS pfnVBoxDispMpGetCallbacks;
973 pfnVBoxDispMpGetCallbacks = (PFNVBOXDISPMP_GETCALLBACKS)GetProcAddress(hVBoxD3D, TEXT("VBoxDispMpGetCallbacks"));
974 if (pfnVBoxDispMpGetCallbacks)
975 {
976 hr = pfnVBoxDispMpGetCallbacks(VBOXDISPMP_VERSION, &VBoxDispMpTstCallbacks);
977 if (S_OK==hr)
978 {
979 CRASSERT(VBoxDispMpTstCallbacks.pfnEnableEvents);
980 CRASSERT(VBoxDispMpTstCallbacks.pfnDisableEvents);
981 CRASSERT(VBoxDispMpTstCallbacks.pfnGetRegions);
982
983 hr = VBoxDispMpTstCallbacks.pfnEnableEvents();
984 if (hr != S_OK)
985 {
986 crWarning("VBoxDispMpTstCallbacks.pfnEnableEvents failed");
987 }
988 else
989 {
990 crDebug("running with VBoxDispD3D");
991 stub.trackWindowVisibleRgn = 0;
992 stub.bRunningUnderWDDM = true;
993 }
994 }
995 else
996 {
997 crWarning("VBoxDispMpGetCallbacks failed");
998 }
999 }
1000 }
1001# endif
1002#endif
1003
1004 crLockMutex(&stub.mutex);
1005 stub.spu->dispatch_table.VBoxPackSetInjectThread();
1006 crUnlockMutex(&stub.mutex);
1007
1008 RTThreadUserSignal(ThreadSelf);
1009
1010 while(!stub.bShutdownSyncThread)
1011 {
1012#ifdef WINDOWS
1013 if (!PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
1014 {
1015# ifdef VBOX_WITH_WDDM
1016 if (VBoxDispMpTstCallbacks.pfnGetRegions)
1017 {
1018 hr = VBoxDispMpTstCallbacks.pfnGetRegions(&Regions, 50);
1019 if (S_OK==hr)
1020 {
1021# if 0
1022 uint32_t i;
1023 crDebug(">>>Regions for HWND(0x%x)>>>", Regions.hWnd);
1024 crDebug("Flags(0x%x)", Regions.pRegions->fFlags.Value);
1025 for (i = 0; i < Regions.pRegions->RectsInfo.cRects; ++i)
1026 {
1027 RECT *pRect = &Regions.pRegions->RectsInfo.aRects[i];
1028 crDebug("Rect(%d): left(%d), top(%d), right(%d), bottom(%d)", i, pRect->left, pRect->top, pRect->right, pRect->bottom);
1029 }
1030 crDebug("<<<<<");
1031# endif
1032 /*hacky way to make sure window wouldn't be deleted in another thread as we hold hashtable lock here*/
1033 crHashtableWalk(stub.windowTable, stubSyncTrUpdateWindowCB, &Regions);
1034 }
1035 else
1036 {
1037 if (WAIT_TIMEOUT!=hr)
1038 {
1039 crWarning("VBoxDispMpTstCallbacks.pfnGetRegions failed with 0x%x", hr);
1040 }
1041 crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
1042 }
1043 }
1044 else
1045# endif
1046 {
1047 crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
1048 RTThreadSleep(50);
1049 }
1050 }
1051 else
1052 {
1053 if (WM_QUIT==msg.message)
1054 {
1055 crDebug("Sync thread got WM_QUIT");
1056 break;
1057 }
1058 else
1059 {
1060 TranslateMessage(&msg);
1061 DispatchMessage(&msg);
1062 }
1063 }
1064#else
1065 crLockMutex(&stub.mutex);
1066 crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
1067 crUnlockMutex(&stub.mutex);
1068 RTThreadSleep(50);
1069#endif
1070 }
1071
1072#ifdef VBOX_WITH_WDDM
1073 if (VBoxDispMpTstCallbacks.pfnDisableEvents)
1074 {
1075 VBoxDispMpTstCallbacks.pfnDisableEvents();
1076 }
1077 if (hVBoxD3D)
1078 {
1079 FreeLibrary(hVBoxD3D);
1080 }
1081#endif
1082 crDebug("Sync thread stopped");
1083 return 0;
1084}
1085#endif
1086
1087/**
1088 * Do one-time initializations for the faker.
1089 * Returns TRUE on success, FALSE otherwise.
1090 */
1091bool
1092stubInit(void)
1093{
1094 /* Here is where we contact the mothership to find out what we're supposed
1095 * to be doing. Networking code in a DLL initializer. I sure hope this
1096 * works :)
1097 *
1098 * HOW can I pass the mothership address to this if I already know it?
1099 */
1100
1101 CRConnection *conn = NULL;
1102 char response[1024];
1103 char **spuchain;
1104 int num_spus;
1105 int *spu_ids;
1106 char **spu_names;
1107 const char *app_id;
1108 int i;
1109 int disable_sync = 0;
1110
1111 if (stub_initialized)
1112 return true;
1113
1114 stubInitVars();
1115
1116 crGetProcName(response, 1024);
1117 crDebug("Stub launched for %s", response);
1118
1119#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
1120 /*@todo when vm boots with compiz turned on, new code causes hang in xcb_wait_for_reply in the sync thread*/
1121 if (!crStrcmp(response, "compiz") || !crStrcmp(response, "compiz_real")
1122 || !crStrcmp(response, "compiz-bin"))
1123 {
1124 disable_sync = 1;
1125 }
1126#endif
1127
1128 /* @todo check if it'd be of any use on other than guests, no use for windows */
1129 app_id = crGetenv( "CR_APPLICATION_ID_NUMBER" );
1130
1131 crNetInit( NULL, NULL );
1132
1133#ifndef WINDOWS
1134 {
1135 CRNetServer ns;
1136
1137 ns.name = "vboxhgcm://host:0";
1138 ns.buffer_size = 1024;
1139 crNetServerConnect(&ns);
1140 if (!ns.conn)
1141 {
1142 crWarning("Failed to connect to host. Make sure 3D acceleration is enabled for this VM.");
1143 return false;
1144 }
1145 else
1146 {
1147 crNetFreeConnection(ns.conn);
1148 }
1149#if 0 && defined(CR_NEWWINTRACK)
1150 {
1151 Status st = XInitThreads();
1152 if (st==0)
1153 {
1154 crWarning("XInitThreads returned %i", (int)st);
1155 }
1156 }
1157#endif
1158 }
1159#endif
1160
1161 strcpy(response, "2 0 feedback 1 pack");
1162 spuchain = crStrSplit( response, " " );
1163 num_spus = crStrToInt( spuchain[0] );
1164 spu_ids = (int *) crAlloc( num_spus * sizeof( *spu_ids ) );
1165 spu_names = (char **) crAlloc( num_spus * sizeof( *spu_names ) );
1166 for (i = 0 ; i < num_spus ; i++)
1167 {
1168 spu_ids[i] = crStrToInt( spuchain[2*i+1] );
1169 spu_names[i] = crStrdup( spuchain[2*i+2] );
1170 crDebug( "SPU %d/%d: (%d) \"%s\"", i+1, num_spus, spu_ids[i], spu_names[i] );
1171 }
1172
1173 stubSetDefaultConfigurationOptions();
1174
1175 stub.spu = crSPULoadChain( num_spus, spu_ids, spu_names, stub.spu_dir, NULL );
1176
1177 crFree( spuchain );
1178 crFree( spu_ids );
1179 for (i = 0; i < num_spus; ++i)
1180 crFree(spu_names[i]);
1181 crFree( spu_names );
1182
1183 // spu chain load failed somewhere
1184 if (!stub.spu) {
1185 return false;
1186 }
1187
1188 crSPUInitDispatchTable( &glim );
1189
1190 /* This is unlikely to change -- We still want to initialize our dispatch
1191 * table with the functions of the first SPU in the chain. */
1192 stubInitSPUDispatch( stub.spu );
1193
1194 /* we need to plug one special stub function into the dispatch table */
1195 glim.GetChromiumParametervCR = stub_GetChromiumParametervCR;
1196
1197#if !defined(VBOX_NO_NATIVEGL)
1198 /* Load pointers to native OpenGL functions into stub.nativeDispatch */
1199 stubInitNativeDispatch();
1200#endif
1201
1202/*crDebug("stub init");
1203raise(SIGINT);*/
1204
1205#ifdef WINDOWS
1206# ifndef CR_NEWWINTRACK
1207 stubInstallWindowMessageHook();
1208# endif
1209#endif
1210
1211#ifdef CR_NEWWINTRACK
1212 {
1213 int rc;
1214
1215 RTR3Init();
1216
1217 if (!disable_sync)
1218 {
1219 crDebug("Starting sync thread");
1220
1221 rc = RTThreadCreate(&stub.hSyncThread, stubSyncThreadProc, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "Sync");
1222 if (RT_FAILURE(rc))
1223 {
1224 crError("Failed to start sync thread! (%x)", rc);
1225 }
1226 RTThreadUserWait(stub.hSyncThread, 60 * 1000);
1227 RTThreadUserReset(stub.hSyncThread);
1228
1229 crDebug("Going on");
1230 }
1231 }
1232#endif
1233
1234#ifdef GLX
1235 stub.xshmSI.shmid = -1;
1236 stub.bShmInitFailed = GL_FALSE;
1237 stub.pGLXPixmapsHash = crAllocHashtable();
1238
1239 stub.bXExtensionsChecked = GL_FALSE;
1240 stub.bHaveXComposite = GL_FALSE;
1241 stub.bHaveXFixes = GL_FALSE;
1242#endif
1243
1244 stub_initialized = 1;
1245 return true;
1246}
1247
1248/* Sigh -- we can't do initialization at load time, since Windows forbids
1249 * the loading of other libraries from DLLMain. */
1250
1251#ifdef LINUX
1252/* GCC crap
1253 *void (*stub_init_ptr)(void) __attribute__((section(".ctors"))) = __stubInit; */
1254#endif
1255
1256#ifdef WINDOWS
1257#define WIN32_LEAN_AND_MEAN
1258#include <windows.h>
1259
1260/* Windows crap */
1261BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
1262{
1263 (void) lpvReserved;
1264
1265 switch (fdwReason)
1266 {
1267 case DLL_PROCESS_ATTACH:
1268 {
1269 CRNetServer ns;
1270
1271 crNetInit(NULL, NULL);
1272 ns.name = "vboxhgcm://host:0";
1273 ns.buffer_size = 1024;
1274 crNetServerConnect(&ns);
1275 if (!ns.conn)
1276 {
1277 crDebug("Failed to connect to host (is guest 3d acceleration enabled?), aborting ICD load.");
1278 return FALSE;
1279 }
1280 else
1281 crNetFreeConnection(ns.conn);
1282
1283 break;
1284 }
1285
1286 case DLL_PROCESS_DETACH:
1287 {
1288 stubSPUSafeTearDown();
1289 break;
1290 }
1291
1292#if 0
1293 case DLL_THREAD_ATTACH:
1294 {
1295 if (stub_initialized)
1296 {
1297 CRASSERT(stub.spu);
1298 stub.spu->dispatch_table.VBoxPackAttachThread();
1299 }
1300 break;
1301 }
1302
1303 case DLL_THREAD_DETACH:
1304 {
1305 if (stub_initialized)
1306 {
1307 CRASSERT(stub.spu);
1308 stub.spu->dispatch_table.VBoxPackDetachThread();
1309 }
1310 break;
1311 }
1312#endif
1313
1314 default:
1315 break;
1316 }
1317
1318 return TRUE;
1319}
1320#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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