VirtualBox

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

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

wddm/3d: fix initial size

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 31.0 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(CR_NEWWINTRACK)
378 crUnlockMutex(mutex);
379# if defined(WINDOWS)
380 if (RTThreadGetState(stub.hSyncThread)!=RTTHREADSTATE_TERMINATED)
381 {
382 ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
383 if (PostThreadMessage(RTThreadGetNative(stub.hSyncThread), WM_QUIT, 0, 0))
384 {
385 RTThreadWait(stub.hSyncThread, 1000, NULL);
386 }
387 else
388 {
389 crDebug("Sync thread killed before DLL_PROCESS_DETACH");
390 }
391 }
392#else
393 ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
394 {
395 /*RTThreadWait might return too early, which cause our code being unloaded while RT thread wrapper is still running*/
396 int rc = pthread_join(RTThreadGetNative(stub.hSyncThread), NULL);
397 if (!rc)
398 {
399 crDebug("pthread_join failed %i", rc);
400 }
401 }
402#endif
403 crLockMutex(mutex);
404#endif
405
406 crNetTearDown();
407#ifdef CHROMIUM_THREADSAFE
408 crUnlockMutex(mutex);
409 crFreeMutex(mutex);
410#endif
411 crMemset(&stub, 0, sizeof(stub));
412}
413
414
415static void stubExitHandler(void)
416{
417 stubSPUSafeTearDown();
418}
419
420/**
421 * Called when we receive a SIGTERM signal.
422 */
423static void stubSignalHandler(int signo)
424{
425 stubSPUSafeTearDown();
426 exit(0); /* this causes stubExitHandler() to be called */
427}
428
429
430/**
431 * Init variables in the stub structure, install signal handler.
432 */
433static void stubInitVars(void)
434{
435 WindowInfo *defaultWin;
436
437#ifdef CHROMIUM_THREADSAFE
438 crInitMutex(&stub.mutex);
439#endif
440
441 /* At the very least we want CR_RGB_BIT. */
442 stub.haveNativeOpenGL = GL_FALSE;
443 stub.spu = NULL;
444 stub.appDrawCursor = 0;
445 stub.minChromiumWindowWidth = 0;
446 stub.minChromiumWindowHeight = 0;
447 stub.maxChromiumWindowWidth = 0;
448 stub.maxChromiumWindowHeight = 0;
449 stub.matchChromiumWindowCount = 0;
450 stub.matchChromiumWindowID = NULL;
451 stub.matchWindowTitle = NULL;
452 stub.ignoreFreeglutMenus = 0;
453 stub.threadSafe = GL_FALSE;
454 stub.trackWindowSize = 0;
455 stub.trackWindowPos = 0;
456 stub.trackWindowVisibility = 0;
457 stub.trackWindowVisibleRgn = 0;
458 stub.mothershipPID = 0;
459 stub.spu_dir = NULL;
460
461 stub.freeContextNumber = MAGIC_CONTEXT_BASE;
462 stub.contextTable = crAllocHashtable();
463 stub.currentContext = NULL;
464
465 stub.windowTable = crAllocHashtable();
466
467#ifdef CR_NEWWINTRACK
468 stub.bShutdownSyncThread = false;
469 stub.hSyncThread = NIL_RTTHREAD;
470#endif
471
472 defaultWin = (WindowInfo *) crCalloc(sizeof(WindowInfo));
473 defaultWin->type = CHROMIUM;
474 defaultWin->spuWindow = 0; /* window 0 always exists */
475#ifdef WINDOWS
476 defaultWin->hVisibleRegion = INVALID_HANDLE_VALUE;
477#elif defined(GLX)
478 defaultWin->pVisibleRegions = NULL;
479 defaultWin->cVisibleRegions = 0;
480#endif
481 crHashtableAdd(stub.windowTable, 0, defaultWin);
482
483#if 1
484 atexit(stubExitHandler);
485 signal(SIGTERM, stubSignalHandler);
486 signal(SIGINT, stubSignalHandler);
487#ifndef WINDOWS
488 signal(SIGPIPE, SIG_IGN); /* the networking code should catch this */
489#endif
490#else
491 (void) stubExitHandler;
492 (void) stubSignalHandler;
493#endif
494}
495
496
497/**
498 * Return a free port number for the mothership to use, or -1 if we
499 * can't find one.
500 */
501static int
502GenerateMothershipPort(void)
503{
504 const int MAX_PORT = 10100;
505 unsigned short port;
506
507 /* generate initial port number randomly */
508 crRandAutoSeed();
509 port = (unsigned short) crRandInt(10001, MAX_PORT);
510
511#ifdef WINDOWS
512 /* XXX should implement a free port check here */
513 return port;
514#else
515 /*
516 * See if this port number really is free, try another if needed.
517 */
518 {
519 struct sockaddr_in servaddr;
520 int so_reuseaddr = 1;
521 int sock, k;
522
523 /* create socket */
524 sock = socket(AF_INET, SOCK_STREAM, 0);
525 CRASSERT(sock > 2);
526
527 /* deallocate socket/port when we exit */
528 k = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
529 (char *) &so_reuseaddr, sizeof(so_reuseaddr));
530 CRASSERT(k == 0);
531
532 /* initialize the servaddr struct */
533 crMemset(&servaddr, 0, sizeof(servaddr) );
534 servaddr.sin_family = AF_INET;
535 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
536
537 while (port < MAX_PORT) {
538 /* Bind to the given port number, return -1 if we fail */
539 servaddr.sin_port = htons((unsigned short) port);
540 k = bind(sock, (struct sockaddr *) &servaddr, sizeof(servaddr));
541 if (k) {
542 /* failed to create port. try next one. */
543 port++;
544 }
545 else {
546 /* free the socket/port now so mothership can make it */
547 close(sock);
548 return port;
549 }
550 }
551 }
552#endif /* WINDOWS */
553 return -1;
554}
555
556
557/**
558 * Try to determine which mothership configuration to use for this program.
559 */
560static char **
561LookupMothershipConfig(const char *procName)
562{
563 const int procNameLen = crStrlen(procName);
564 FILE *f;
565 const char *home;
566 char configPath[1000];
567
568 /* first, check if the CR_CONFIG env var is set */
569 {
570 const char *conf = crGetenv("CR_CONFIG");
571 if (conf && crStrlen(conf) > 0)
572 return crStrSplit(conf, " ");
573 }
574
575 /* second, look up config name from config file */
576 home = crGetenv("HOME");
577 if (home)
578 sprintf(configPath, "%s/%s", home, CONFIG_LOOKUP_FILE);
579 else
580 crStrcpy(configPath, CONFIG_LOOKUP_FILE); /* from current dir */
581 /* Check if the CR_CONFIG_PATH env var is set. */
582 {
583 const char *conf = crGetenv("CR_CONFIG_PATH");
584 if (conf)
585 crStrcpy(configPath, conf); /* from env var */
586 }
587
588 f = fopen(configPath, "r");
589 if (!f) {
590 return NULL;
591 }
592
593 while (!feof(f)) {
594 char line[1000];
595 char **args;
596 fgets(line, 999, f);
597 line[crStrlen(line) - 1] = 0; /* remove trailing newline */
598 if (crStrncmp(line, procName, procNameLen) == 0 &&
599 (line[procNameLen] == ' ' || line[procNameLen] == '\t'))
600 {
601 crWarning("Using Chromium configuration for %s from %s",
602 procName, configPath);
603 args = crStrSplit(line + procNameLen + 1, " ");
604 return args;
605 }
606 }
607 fclose(f);
608 return NULL;
609}
610
611
612static int Mothership_Awake = 0;
613
614
615/**
616 * Signal handler to determine when mothership is ready.
617 */
618static void
619MothershipPhoneHome(int signo)
620{
621 crDebug("Got signal %d: mothership is awake!", signo);
622 Mothership_Awake = 1;
623}
624
625void stubSetDefaultConfigurationOptions(void)
626{
627 unsigned char key[16]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
628
629 stub.appDrawCursor = 0;
630 stub.minChromiumWindowWidth = 0;
631 stub.minChromiumWindowHeight = 0;
632 stub.maxChromiumWindowWidth = 0;
633 stub.maxChromiumWindowHeight = 0;
634 stub.matchChromiumWindowID = NULL;
635 stub.numIgnoreWindowID = 0;
636 stub.matchWindowTitle = NULL;
637 stub.ignoreFreeglutMenus = 0;
638 stub.trackWindowSize = 1;
639 stub.trackWindowPos = 1;
640 stub.trackWindowVisibility = 1;
641 stub.trackWindowVisibleRgn = 1;
642 stub.matchChromiumWindowCount = 0;
643 stub.spu_dir = NULL;
644 crNetSetRank(0);
645 crNetSetContextRange(32, 35);
646 crNetSetNodeRange("iam0", "iamvis20");
647 crNetSetKey(key,sizeof(key));
648 stub.force_pbuffers = 0;
649 stub.viewportHack = 0;
650
651#ifdef WINDOWS
652 {
653 char name[1000];
654 int i;
655
656 /* Apply viewport hack only if we're running under wine */
657 if (NULL!=GetModuleHandle("wined3d.dll"))
658 {
659 crGetProcName(name, 1000);
660 for (i=0; gsViewportHackApps[i]; ++i)
661 {
662 if (!stricmp(name, gsViewportHackApps[i]))
663 {
664 stub.viewportHack = 1;
665 break;
666 }
667 }
668 }
669 }
670#endif
671}
672
673#ifdef CR_NEWWINTRACK
674# ifdef VBOX_WITH_WDDM
675static stubDispatchVisibleRegions(WindowInfo *pWindow)
676{
677 DWORD dwCount;
678 LPRGNDATA lpRgnData;
679
680 dwCount = GetRegionData(pWindow->hVisibleRegion, 0, NULL);
681 lpRgnData = crAlloc(dwCount);
682
683 if (lpRgnData)
684 {
685 GetRegionData(pWindow->hVisibleRegion, dwCount, lpRgnData);
686 crDebug("Dispatched WindowVisibleRegion (%i, cRects=%i)", pWindow->spuWindow, lpRgnData->rdh.nCount);
687 stub.spuDispatch.WindowVisibleRegion(pWindow->spuWindow, lpRgnData->rdh.nCount, (GLint*) lpRgnData->Buffer);
688 crFree(lpRgnData);
689 }
690 else crWarning("GetRegionData failed, VisibleRegions update failed");
691}
692
693static HRGN stubMakeRegionFromRects(PVBOXVIDEOCM_CMD_RECTS pRegions, uint32_t start)
694{
695 HRGN hRgn, hTmpRgn;
696 uint32_t i;
697
698 if (pRegions->RectsInfo.cRects<=start)
699 {
700 return INVALID_HANDLE_VALUE;
701 }
702
703 hRgn = CreateRectRgn(0, 0, 0, 0);
704 for (i=start; i<pRegions->RectsInfo.cRects; ++i)
705 {
706 hTmpRgn = CreateRectRgnIndirect(&pRegions->RectsInfo.aRects[i]);
707 CombineRgn(hRgn, hRgn, hTmpRgn, RGN_OR);
708 DeleteObject(hTmpRgn);
709 }
710 return hRgn;
711}
712
713static void stubSyncTrUpdateWindowCB(unsigned long key, void *data1, void *data2)
714{
715 WindowInfo *pWindow = (WindowInfo *) data1;
716 VBOXDISPMP_REGIONS *pRegions = (VBOXDISPMP_REGIONS*) data2;
717 bool bChanged = false;
718 HRGN hNewRgn = INVALID_HANDLE_VALUE;
719
720 if (pRegions->hWnd != pWindow->hWnd)
721 {
722 return;
723 }
724
725 if (!pWindow->mapped)
726 {
727 pWindow->mapped = GL_TRUE;
728 bChanged = true;
729 crDebug("Dispatched: WindowShow(%i, %i)", pWindow->spuWindow, pWindow->mapped);
730 stub.spu->dispatch_table.WindowShow(pWindow->spuWindow, pWindow->mapped);
731 }
732
733 if (pRegions->pRegions->fFlags.bSetVisibleRects && pRegions->pRegions->fFlags.bSetViewRect)
734 {
735 int winX, winY;
736 unsigned int winW, winH;
737
738 winX = pRegions->pRegions->RectsInfo.aRects[0].left;
739 winY = pRegions->pRegions->RectsInfo.aRects[0].top;
740 winW = pRegions->pRegions->RectsInfo.aRects[0].right - winX;
741 winH = pRegions->pRegions->RectsInfo.aRects[0].bottom - winY;
742
743 if (stub.trackWindowPos && (winX!=pWindow->x || winY!=pWindow->y))
744 {
745 crDebug("Dispatched WindowPosition (%i)", pWindow->spuWindow);
746 stub.spuDispatch.WindowPosition(pWindow->spuWindow, winX, winY);
747 pWindow->x = winX;
748 pWindow->y = winY;
749 bChanged = true;
750 }
751
752 if (stub.trackWindowSize && (winW!=pWindow->width || winH!=pWindow->height))
753 {
754 crDebug("Dispatched WindowSize (%i)", pWindow->spuWindow);
755 stub.spuDispatch.WindowSize(pWindow->spuWindow, winW, winH);
756 pWindow->width = winW;
757 pWindow->height = winH;
758 bChanged = true;
759 }
760
761 hNewRgn = stubMakeRegionFromRects(pRegions->pRegions, 1);
762
763#if 0
764 /* may be needed in the future for proper resize handling */
765 {
766 BOOL bRc = MoveWindow(pRegions->hWnd, winX, winY, winW, winH, FALSE /*BOOL bRepaint*/);
767 if (!bRc)
768 {
769 DWORD winEr = GetLastError();
770 crWarning("stubSyncTrUpdateWindowCB: MoveWindow failed winEr(%d)", winEr);
771 }
772 }
773#endif
774 }
775 else
776 {
777 hNewRgn = stubMakeRegionFromRects(pRegions->pRegions, 0);
778 }
779
780 if (hNewRgn!=INVALID_HANDLE_VALUE)
781 {
782 OffsetRgn(hNewRgn, -pWindow->x, -pWindow->y);
783
784 if (pWindow->hVisibleRegion!=INVALID_HANDLE_VALUE)
785 {
786 CombineRgn(hNewRgn, pWindow->hVisibleRegion, hNewRgn,
787 pRegions->pRegions->fFlags.bAddHiddenRects ? RGN_DIFF:RGN_OR);
788
789 if (!EqualRgn(pWindow->hVisibleRegion, hNewRgn))
790 {
791 DeleteObject(pWindow->hVisibleRegion);
792 pWindow->hVisibleRegion = hNewRgn;
793 stubDispatchVisibleRegions(pWindow);
794 bChanged = true;
795 }
796 else
797 {
798 DeleteObject(hNewRgn);
799 }
800 }
801 else
802 {
803 if (pRegions->pRegions->fFlags.bSetVisibleRects)
804 {
805 pWindow->hVisibleRegion = hNewRgn;
806 stubDispatchVisibleRegions(pWindow);
807 bChanged = true;
808 }
809 }
810 }
811
812 if (bChanged)
813 {
814 stub.spu->dispatch_table.Flush();
815 }
816}
817# endif
818
819static void stubSyncTrCheckWindowsCB(unsigned long key, void *data1, void *data2)
820{
821 WindowInfo *pWindow = (WindowInfo *) data1;
822 (void) data2;
823
824 if (pWindow->type!=CHROMIUM || pWindow->spuWindow==0)
825 {
826 return;
827 }
828
829 stub.spu->dispatch_table.VBoxPackSetInjectID(pWindow->u32ClientID);
830
831 if (!stubSystemWindowExist(pWindow))
832 {
833#ifdef WINDOWS
834 crWindowDestroy((GLint)pWindow->hWnd);
835#else
836 crWindowDestroy((GLint)pWindow->drawable);
837#endif
838 /*No need to flush here as crWindowDestroy does it*/
839 return;
840 }
841
842 stubCheckWindowState(pWindow, GL_TRUE);
843}
844
845static DECLCALLBACK(int) stubSyncThreadProc(RTTHREAD ThreadSelf, void *pvUser)
846{
847#ifdef WINDOWS
848 MSG msg;
849# ifdef VBOX_WITH_WDDM
850 static VBOXDISPMP_CALLBACKS VBoxDispMpTstCallbacks = {NULL, NULL, NULL};
851 HMODULE hVBoxD3D = NULL;
852 VBOXDISPMP_REGIONS Regions;
853 HRESULT hr;
854# endif
855#endif
856
857 (void) pvUser;
858
859 crDebug("Sync thread started");
860#ifdef WINDOWS
861 PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
862# ifdef VBOX_WITH_WDDM
863 if (GetModuleHandleEx(0, "VBoxDispD3D", &hVBoxD3D))
864 {
865 PFNVBOXDISPMP_GETCALLBACKS pfnVBoxDispMpGetCallbacks;
866 pfnVBoxDispMpGetCallbacks = (PFNVBOXDISPMP_GETCALLBACKS)GetProcAddress(hVBoxD3D, TEXT("VBoxDispMpGetCallbacks"));
867 if (pfnVBoxDispMpGetCallbacks)
868 {
869 hr = pfnVBoxDispMpGetCallbacks(VBOXDISPMP_VERSION, &VBoxDispMpTstCallbacks);
870 if (S_OK==hr)
871 {
872 CRASSERT(VBoxDispMpTstCallbacks.pfnEnableEvents);
873 CRASSERT(VBoxDispMpTstCallbacks.pfnDisableEvents);
874 CRASSERT(VBoxDispMpTstCallbacks.pfnGetRegions);
875
876 hr = VBoxDispMpTstCallbacks.pfnEnableEvents();
877 if (hr != S_OK)
878 {
879 crWarning("VBoxDispMpTstCallbacks.pfnEnableEvents failed");
880 }
881 else
882 {
883 crDebug("running with VBoxDispD3D");
884 }
885 }
886 else
887 {
888 crWarning("VBoxDispMpGetCallbacks failed");
889 }
890 }
891 }
892# endif
893#endif
894
895 crLockMutex(&stub.mutex);
896 stub.spu->dispatch_table.VBoxPackSetInjectThread();
897 crUnlockMutex(&stub.mutex);
898
899 RTThreadUserSignal(ThreadSelf);
900
901 while(!stub.bShutdownSyncThread)
902 {
903#ifdef WINDOWS
904 if (!PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
905 {
906# ifdef VBOX_WITH_WDDM
907 if (VBoxDispMpTstCallbacks.pfnGetRegions)
908 {
909 hr = VBoxDispMpTstCallbacks.pfnGetRegions(&Regions, 50);
910 if (S_OK==hr)
911 {
912# if 0
913 uint32_t i;
914 crDebug(">>>Regions for HWND(0x%x)>>>", Regions.hWnd);
915 crDebug("Flags(0x%x)", Regions.pRegions->fFlags.Value);
916 for (i = 0; i < Regions.pRegions->RectsInfo.cRects; ++i)
917 {
918 RECT *pRect = &Regions.pRegions->RectsInfo.aRects[i];
919 crDebug("Rect(%d): left(%d), top(%d), right(%d), bottom(%d)", i, pRect->left, pRect->top, pRect->right, pRect->bottom);
920 }
921 crDebug("<<<<<");
922# endif
923 /*hacky way to make sure window wouldn't be deleted in another thread as we hold hashtable lock here*/
924 crHashtableWalk(stub.windowTable, stubSyncTrUpdateWindowCB, &Regions);
925 }
926 else
927 {
928 if (WAIT_TIMEOUT!=hr)
929 {
930 crWarning("VBoxDispMpTstCallbacks.pfnGetRegions failed with 0x%x", hr);
931 }
932 }
933 }
934 else
935# endif
936 {
937 crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
938 RTThreadSleep(50);
939 }
940 }
941 else
942 {
943 if (WM_QUIT==msg.message)
944 {
945 crDebug("Sync thread got WM_QUIT");
946 break;
947 }
948 else
949 {
950 TranslateMessage(&msg);
951 DispatchMessage(&msg);
952 }
953 }
954#else
955 crLockMutex(&stub.mutex);
956 crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
957 crUnlockMutex(&stub.mutex);
958 RTThreadSleep(50);
959#endif
960 }
961
962#ifdef VBOX_WITH_WDDM
963 if (VBoxDispMpTstCallbacks.pfnDisableEvents)
964 {
965 VBoxDispMpTstCallbacks.pfnDisableEvents();
966 }
967 if (hVBoxD3D)
968 {
969 FreeLibrary(hVBoxD3D);
970 }
971#endif
972 crDebug("Sync thread stopped");
973 return 0;
974}
975#endif
976
977/**
978 * Do one-time initializations for the faker.
979 * Returns TRUE on success, FALSE otherwise.
980 */
981bool
982stubInit(void)
983{
984 /* Here is where we contact the mothership to find out what we're supposed
985 * to be doing. Networking code in a DLL initializer. I sure hope this
986 * works :)
987 *
988 * HOW can I pass the mothership address to this if I already know it?
989 */
990
991 CRConnection *conn = NULL;
992 char response[1024];
993 char **spuchain;
994 int num_spus;
995 int *spu_ids;
996 char **spu_names;
997 const char *app_id;
998 int i;
999 int disable_sync = 0;
1000
1001 if (stub_initialized)
1002 return true;
1003
1004 stubInitVars();
1005
1006 crGetProcName(response, 1024);
1007 crDebug("Stub launched for %s", response);
1008
1009#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
1010 /*@todo when vm boots with compiz turned on, new code causes hang in xcb_wait_for_reply in the sync thread*/
1011 if (!crStrcmp(response, "compiz") || !crStrcmp(response, "compiz_real")
1012 || !crStrcmp(response, "compiz-bin"))
1013 {
1014 disable_sync = 1;
1015 }
1016#endif
1017
1018 /* @todo check if it'd be of any use on other than guests, no use for windows */
1019 app_id = crGetenv( "CR_APPLICATION_ID_NUMBER" );
1020
1021 crNetInit( NULL, NULL );
1022
1023#ifndef WINDOWS
1024 {
1025 CRNetServer ns;
1026
1027 ns.name = "vboxhgcm://host:0";
1028 ns.buffer_size = 1024;
1029 crNetServerConnect(&ns);
1030 if (!ns.conn)
1031 {
1032 crWarning("Failed to connect to host. Make sure 3D acceleration is enabled for this VM.");
1033 return false;
1034 }
1035 else
1036 {
1037 crNetFreeConnection(ns.conn);
1038 }
1039#if 0 && defined(CR_NEWWINTRACK)
1040 {
1041 Status st = XInitThreads();
1042 if (st==0)
1043 {
1044 crWarning("XInitThreads returned %i", (int)st);
1045 }
1046 }
1047#endif
1048 }
1049#endif
1050
1051 strcpy(response, "2 0 feedback 1 pack");
1052 spuchain = crStrSplit( response, " " );
1053 num_spus = crStrToInt( spuchain[0] );
1054 spu_ids = (int *) crAlloc( num_spus * sizeof( *spu_ids ) );
1055 spu_names = (char **) crAlloc( num_spus * sizeof( *spu_names ) );
1056 for (i = 0 ; i < num_spus ; i++)
1057 {
1058 spu_ids[i] = crStrToInt( spuchain[2*i+1] );
1059 spu_names[i] = crStrdup( spuchain[2*i+2] );
1060 crDebug( "SPU %d/%d: (%d) \"%s\"", i+1, num_spus, spu_ids[i], spu_names[i] );
1061 }
1062
1063 stubSetDefaultConfigurationOptions();
1064
1065 stub.spu = crSPULoadChain( num_spus, spu_ids, spu_names, stub.spu_dir, NULL );
1066
1067 crFree( spuchain );
1068 crFree( spu_ids );
1069 for (i = 0; i < num_spus; ++i)
1070 crFree(spu_names[i]);
1071 crFree( spu_names );
1072
1073 // spu chain load failed somewhere
1074 if (!stub.spu) {
1075 return false;
1076 }
1077
1078 crSPUInitDispatchTable( &glim );
1079
1080 /* This is unlikely to change -- We still want to initialize our dispatch
1081 * table with the functions of the first SPU in the chain. */
1082 stubInitSPUDispatch( stub.spu );
1083
1084 /* we need to plug one special stub function into the dispatch table */
1085 glim.GetChromiumParametervCR = stub_GetChromiumParametervCR;
1086
1087#if !defined(VBOX_NO_NATIVEGL)
1088 /* Load pointers to native OpenGL functions into stub.nativeDispatch */
1089 stubInitNativeDispatch();
1090#endif
1091
1092/*crDebug("stub init");
1093raise(SIGINT);*/
1094
1095#ifdef WINDOWS
1096# ifndef CR_NEWWINTRACK
1097 stubInstallWindowMessageHook();
1098# endif
1099#endif
1100
1101#ifdef CR_NEWWINTRACK
1102 {
1103 int rc;
1104
1105 RTR3Init();
1106
1107 if (!disable_sync)
1108 {
1109 crDebug("Starting sync thread");
1110
1111 rc = RTThreadCreate(&stub.hSyncThread, stubSyncThreadProc, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "Sync");
1112 if (RT_FAILURE(rc))
1113 {
1114 crError("Failed to start sync thread! (%x)", rc);
1115 }
1116 RTThreadUserWait(stub.hSyncThread, 60 * 1000);
1117 RTThreadUserReset(stub.hSyncThread);
1118
1119 crDebug("Going on");
1120 }
1121 }
1122#endif
1123
1124#ifdef GLX
1125 stub.xshmSI.shmid = -1;
1126 stub.bShmInitFailed = GL_FALSE;
1127 stub.pGLXPixmapsHash = crAllocHashtable();
1128#endif
1129
1130 stub_initialized = 1;
1131 return true;
1132}
1133
1134/* Sigh -- we can't do initialization at load time, since Windows forbids
1135 * the loading of other libraries from DLLMain. */
1136
1137#ifdef LINUX
1138/* GCC crap
1139 *void (*stub_init_ptr)(void) __attribute__((section(".ctors"))) = __stubInit; */
1140#endif
1141
1142#ifdef WINDOWS
1143#define WIN32_LEAN_AND_MEAN
1144#include <windows.h>
1145
1146/* Windows crap */
1147BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
1148{
1149 (void) lpvReserved;
1150
1151 switch (fdwReason)
1152 {
1153 case DLL_PROCESS_ATTACH:
1154 {
1155 CRNetServer ns;
1156
1157 crNetInit(NULL, NULL);
1158 ns.name = "vboxhgcm://host:0";
1159 ns.buffer_size = 1024;
1160 crNetServerConnect(&ns);
1161 if (!ns.conn)
1162 {
1163 crDebug("Failed to connect to host (is guest 3d acceleration enabled?), aborting ICD load.");
1164 return FALSE;
1165 }
1166 else
1167 crNetFreeConnection(ns.conn);
1168
1169 break;
1170 }
1171
1172 case DLL_PROCESS_DETACH:
1173 {
1174 stubSPUSafeTearDown();
1175 break;
1176 }
1177
1178 case DLL_THREAD_ATTACH:
1179 break;
1180
1181 case DLL_THREAD_DETACH:
1182 break;
1183
1184 default:
1185 break;
1186 }
1187
1188 return TRUE;
1189}
1190#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