VirtualBox

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

Last change on this file since 30513 was 30513, checked in by vboxsync, 15 years ago

crOpenGL: window tracking with wddm callbacks

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

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