VirtualBox

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

Last change on this file since 35263 was 35127, checked in by vboxsync, 13 years ago

crOpenGL: fix OpenGL support for win2k guests

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 34.3 KB
Line 
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 (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 OffsetRgn(hNewRgn, -pWindow->x, -pWindow->y);
861
862 if (pWindow->hVisibleRegion!=INVALID_HANDLE_VALUE)
863 {
864 CombineRgn(hNewRgn, pWindow->hVisibleRegion, hNewRgn,
865 pRegions->pRegions->fFlags.bAddHiddenRects ? RGN_DIFF:RGN_OR);
866
867 if (!EqualRgn(pWindow->hVisibleRegion, hNewRgn))
868 {
869 DeleteObject(pWindow->hVisibleRegion);
870 pWindow->hVisibleRegion = hNewRgn;
871 stubDispatchVisibleRegions(pWindow);
872 bChanged = true;
873 }
874 else
875 {
876 DeleteObject(hNewRgn);
877 }
878 }
879 else
880 {
881 if (pRegions->pRegions->fFlags.bSetVisibleRects)
882 {
883 pWindow->hVisibleRegion = hNewRgn;
884 stubDispatchVisibleRegions(pWindow);
885 bChanged = true;
886 }
887 }
888 }
889
890 if (bChanged)
891 {
892 stub.spu->dispatch_table.Flush();
893 }
894}
895# endif
896
897static void stubSyncTrCheckWindowsCB(unsigned long key, void *data1, void *data2)
898{
899 WindowInfo *pWindow = (WindowInfo *) data1;
900 (void) data2;
901
902 if (pWindow->type!=CHROMIUM || pWindow->spuWindow==0)
903 {
904 return;
905 }
906
907 stub.spu->dispatch_table.VBoxPackSetInjectID(pWindow->u32ClientID);
908
909 if (!stubSystemWindowExist(pWindow))
910 {
911#ifdef WINDOWS
912 crWindowDestroy((GLint)pWindow->hWnd);
913#else
914 crWindowDestroy((GLint)pWindow->drawable);
915#endif
916 /*No need to flush here as crWindowDestroy does it*/
917 return;
918 }
919
920#if defined(WINDOWS) && defined(VBOX_WITH_WDDM)
921 if (stub.bRunningUnderWDDM)
922 return;
923#endif
924 stubCheckWindowState(pWindow, GL_TRUE);
925}
926
927static DECLCALLBACK(int) stubSyncThreadProc(RTTHREAD ThreadSelf, void *pvUser)
928{
929#ifdef WINDOWS
930 MSG msg;
931# ifdef VBOX_WITH_WDDM
932 static VBOXDISPMP_CALLBACKS VBoxDispMpTstCallbacks = {NULL, NULL, NULL};
933 HMODULE hVBoxD3D = NULL;
934 VBOXDISPMP_REGIONS Regions;
935 HRESULT hr;
936# endif
937#endif
938
939 (void) pvUser;
940
941 crDebug("Sync thread started");
942#ifdef WINDOWS
943 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
944# ifdef VBOX_WITH_WDDM
945 hVBoxD3D = GetModuleHandle("VBoxDispD3D");
946 if (hVBoxD3D)
947 {
948 hVBoxD3D = LoadLibrary("VBoxDispD3D");
949 }
950
951 if (hVBoxD3D)
952 {
953 PFNVBOXDISPMP_GETCALLBACKS pfnVBoxDispMpGetCallbacks;
954 pfnVBoxDispMpGetCallbacks = (PFNVBOXDISPMP_GETCALLBACKS)GetProcAddress(hVBoxD3D, TEXT("VBoxDispMpGetCallbacks"));
955 if (pfnVBoxDispMpGetCallbacks)
956 {
957 hr = pfnVBoxDispMpGetCallbacks(VBOXDISPMP_VERSION, &VBoxDispMpTstCallbacks);
958 if (S_OK==hr)
959 {
960 CRASSERT(VBoxDispMpTstCallbacks.pfnEnableEvents);
961 CRASSERT(VBoxDispMpTstCallbacks.pfnDisableEvents);
962 CRASSERT(VBoxDispMpTstCallbacks.pfnGetRegions);
963
964 hr = VBoxDispMpTstCallbacks.pfnEnableEvents();
965 if (hr != S_OK)
966 {
967 crWarning("VBoxDispMpTstCallbacks.pfnEnableEvents failed");
968 }
969 else
970 {
971 crDebug("running with VBoxDispD3D");
972 stub.trackWindowVisibleRgn = 0;
973 stub.bRunningUnderWDDM = true;
974 }
975 }
976 else
977 {
978 crWarning("VBoxDispMpGetCallbacks failed");
979 }
980 }
981 }
982# endif
983#endif
984
985 crLockMutex(&stub.mutex);
986 stub.spu->dispatch_table.VBoxPackSetInjectThread();
987 crUnlockMutex(&stub.mutex);
988
989 RTThreadUserSignal(ThreadSelf);
990
991 while(!stub.bShutdownSyncThread)
992 {
993#ifdef WINDOWS
994 if (!PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
995 {
996# ifdef VBOX_WITH_WDDM
997 if (VBoxDispMpTstCallbacks.pfnGetRegions)
998 {
999 hr = VBoxDispMpTstCallbacks.pfnGetRegions(&Regions, 50);
1000 if (S_OK==hr)
1001 {
1002# if 0
1003 uint32_t i;
1004 crDebug(">>>Regions for HWND(0x%x)>>>", Regions.hWnd);
1005 crDebug("Flags(0x%x)", Regions.pRegions->fFlags.Value);
1006 for (i = 0; i < Regions.pRegions->RectsInfo.cRects; ++i)
1007 {
1008 RECT *pRect = &Regions.pRegions->RectsInfo.aRects[i];
1009 crDebug("Rect(%d): left(%d), top(%d), right(%d), bottom(%d)", i, pRect->left, pRect->top, pRect->right, pRect->bottom);
1010 }
1011 crDebug("<<<<<");
1012# endif
1013 /*hacky way to make sure window wouldn't be deleted in another thread as we hold hashtable lock here*/
1014 crHashtableWalk(stub.windowTable, stubSyncTrUpdateWindowCB, &Regions);
1015 }
1016 else
1017 {
1018 if (WAIT_TIMEOUT!=hr)
1019 {
1020 crWarning("VBoxDispMpTstCallbacks.pfnGetRegions failed with 0x%x", hr);
1021 }
1022 crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
1023 }
1024 }
1025 else
1026# endif
1027 {
1028 crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
1029 RTThreadSleep(50);
1030 }
1031 }
1032 else
1033 {
1034 if (WM_QUIT==msg.message)
1035 {
1036 crDebug("Sync thread got WM_QUIT");
1037 break;
1038 }
1039 else
1040 {
1041 TranslateMessage(&msg);
1042 DispatchMessage(&msg);
1043 }
1044 }
1045#else
1046 crLockMutex(&stub.mutex);
1047 crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
1048 crUnlockMutex(&stub.mutex);
1049 RTThreadSleep(50);
1050#endif
1051 }
1052
1053#ifdef VBOX_WITH_WDDM
1054 if (VBoxDispMpTstCallbacks.pfnDisableEvents)
1055 {
1056 VBoxDispMpTstCallbacks.pfnDisableEvents();
1057 }
1058 if (hVBoxD3D)
1059 {
1060 FreeLibrary(hVBoxD3D);
1061 }
1062#endif
1063 crDebug("Sync thread stopped");
1064 return 0;
1065}
1066#endif
1067
1068/**
1069 * Do one-time initializations for the faker.
1070 * Returns TRUE on success, FALSE otherwise.
1071 */
1072bool
1073stubInit(void)
1074{
1075 /* Here is where we contact the mothership to find out what we're supposed
1076 * to be doing. Networking code in a DLL initializer. I sure hope this
1077 * works :)
1078 *
1079 * HOW can I pass the mothership address to this if I already know it?
1080 */
1081
1082 CRConnection *conn = NULL;
1083 char response[1024];
1084 char **spuchain;
1085 int num_spus;
1086 int *spu_ids;
1087 char **spu_names;
1088 const char *app_id;
1089 int i;
1090 int disable_sync = 0;
1091
1092 if (stub_initialized)
1093 return true;
1094
1095 stubInitVars();
1096
1097 crGetProcName(response, 1024);
1098 crDebug("Stub launched for %s", response);
1099
1100#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
1101 /*@todo when vm boots with compiz turned on, new code causes hang in xcb_wait_for_reply in the sync thread*/
1102 if (!crStrcmp(response, "compiz") || !crStrcmp(response, "compiz_real")
1103 || !crStrcmp(response, "compiz-bin"))
1104 {
1105 disable_sync = 1;
1106 }
1107#endif
1108
1109 /* @todo check if it'd be of any use on other than guests, no use for windows */
1110 app_id = crGetenv( "CR_APPLICATION_ID_NUMBER" );
1111
1112 crNetInit( NULL, NULL );
1113
1114#ifndef WINDOWS
1115 {
1116 CRNetServer ns;
1117
1118 ns.name = "vboxhgcm://host:0";
1119 ns.buffer_size = 1024;
1120 crNetServerConnect(&ns);
1121 if (!ns.conn)
1122 {
1123 crWarning("Failed to connect to host. Make sure 3D acceleration is enabled for this VM.");
1124 return false;
1125 }
1126 else
1127 {
1128 crNetFreeConnection(ns.conn);
1129 }
1130#if 0 && defined(CR_NEWWINTRACK)
1131 {
1132 Status st = XInitThreads();
1133 if (st==0)
1134 {
1135 crWarning("XInitThreads returned %i", (int)st);
1136 }
1137 }
1138#endif
1139 }
1140#endif
1141
1142 strcpy(response, "2 0 feedback 1 pack");
1143 spuchain = crStrSplit( response, " " );
1144 num_spus = crStrToInt( spuchain[0] );
1145 spu_ids = (int *) crAlloc( num_spus * sizeof( *spu_ids ) );
1146 spu_names = (char **) crAlloc( num_spus * sizeof( *spu_names ) );
1147 for (i = 0 ; i < num_spus ; i++)
1148 {
1149 spu_ids[i] = crStrToInt( spuchain[2*i+1] );
1150 spu_names[i] = crStrdup( spuchain[2*i+2] );
1151 crDebug( "SPU %d/%d: (%d) \"%s\"", i+1, num_spus, spu_ids[i], spu_names[i] );
1152 }
1153
1154 stubSetDefaultConfigurationOptions();
1155
1156 stub.spu = crSPULoadChain( num_spus, spu_ids, spu_names, stub.spu_dir, NULL );
1157
1158 crFree( spuchain );
1159 crFree( spu_ids );
1160 for (i = 0; i < num_spus; ++i)
1161 crFree(spu_names[i]);
1162 crFree( spu_names );
1163
1164 // spu chain load failed somewhere
1165 if (!stub.spu) {
1166 return false;
1167 }
1168
1169 crSPUInitDispatchTable( &glim );
1170
1171 /* This is unlikely to change -- We still want to initialize our dispatch
1172 * table with the functions of the first SPU in the chain. */
1173 stubInitSPUDispatch( stub.spu );
1174
1175 /* we need to plug one special stub function into the dispatch table */
1176 glim.GetChromiumParametervCR = stub_GetChromiumParametervCR;
1177
1178#if !defined(VBOX_NO_NATIVEGL)
1179 /* Load pointers to native OpenGL functions into stub.nativeDispatch */
1180 stubInitNativeDispatch();
1181#endif
1182
1183/*crDebug("stub init");
1184raise(SIGINT);*/
1185
1186#ifdef WINDOWS
1187# ifndef CR_NEWWINTRACK
1188 stubInstallWindowMessageHook();
1189# endif
1190#endif
1191
1192#ifdef CR_NEWWINTRACK
1193 {
1194 int rc;
1195
1196 RTR3Init();
1197
1198 if (!disable_sync)
1199 {
1200 crDebug("Starting sync thread");
1201
1202 rc = RTThreadCreate(&stub.hSyncThread, stubSyncThreadProc, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "Sync");
1203 if (RT_FAILURE(rc))
1204 {
1205 crError("Failed to start sync thread! (%x)", rc);
1206 }
1207 RTThreadUserWait(stub.hSyncThread, 60 * 1000);
1208 RTThreadUserReset(stub.hSyncThread);
1209
1210 crDebug("Going on");
1211 }
1212 }
1213#endif
1214
1215#ifdef GLX
1216 stub.xshmSI.shmid = -1;
1217 stub.bShmInitFailed = GL_FALSE;
1218 stub.pGLXPixmapsHash = crAllocHashtable();
1219
1220 stub.bXExtensionsChecked = GL_FALSE;
1221 stub.bHaveXComposite = GL_FALSE;
1222 stub.bHaveXFixes = GL_FALSE;
1223#endif
1224
1225 stub_initialized = 1;
1226 return true;
1227}
1228
1229/* Sigh -- we can't do initialization at load time, since Windows forbids
1230 * the loading of other libraries from DLLMain. */
1231
1232#ifdef LINUX
1233/* GCC crap
1234 *void (*stub_init_ptr)(void) __attribute__((section(".ctors"))) = __stubInit; */
1235#endif
1236
1237#ifdef WINDOWS
1238#define WIN32_LEAN_AND_MEAN
1239#include <windows.h>
1240
1241/* Windows crap */
1242BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
1243{
1244 (void) lpvReserved;
1245
1246 switch (fdwReason)
1247 {
1248 case DLL_PROCESS_ATTACH:
1249 {
1250 CRNetServer ns;
1251
1252 crNetInit(NULL, NULL);
1253 ns.name = "vboxhgcm://host:0";
1254 ns.buffer_size = 1024;
1255 crNetServerConnect(&ns);
1256 if (!ns.conn)
1257 {
1258 crDebug("Failed to connect to host (is guest 3d acceleration enabled?), aborting ICD load.");
1259 return FALSE;
1260 }
1261 else
1262 crNetFreeConnection(ns.conn);
1263
1264 break;
1265 }
1266
1267 case DLL_PROCESS_DETACH:
1268 {
1269 stubSPUSafeTearDown();
1270 break;
1271 }
1272
1273#if 0
1274 case DLL_THREAD_ATTACH:
1275 {
1276 if (stub_initialized)
1277 {
1278 CRASSERT(stub.spu);
1279 stub.spu->dispatch_table.VBoxPackAttachThread();
1280 }
1281 break;
1282 }
1283
1284 case DLL_THREAD_DETACH:
1285 {
1286 if (stub_initialized)
1287 {
1288 CRASSERT(stub.spu);
1289 stub.spu->dispatch_table.VBoxPackDetachThread();
1290 }
1291 break;
1292 }
1293#endif
1294
1295 default:
1296 break;
1297 }
1298
1299 return TRUE;
1300}
1301#endif
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use