VirtualBox

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

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

crOgl/wddm: visible regions handling fix

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

© 2023 Oracle
ContactPrivacy policyTerms of Use