VirtualBox

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

最後變更 在這個檔案從21077是 20616,由 vboxsync 提交於 15 年 前

crOpenGL: enable viewport hack for googleearth in d3d9 mode

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 17.5 KB
 
1/* Copyright (c) 2001, Stanford University
2 * All rights reserved
3 *
4 * See the file LICENSE.txt for information on redistributing this software.
5 */
6
7#include "cr_spu.h"
8#include "cr_net.h"
9#include "cr_error.h"
10#include "cr_mem.h"
11#include "cr_string.h"
12#include "cr_net.h"
13#include "cr_environment.h"
14#include "cr_process.h"
15#include "cr_rand.h"
16#include "cr_netserver.h"
17#include "stub.h"
18#include <stdlib.h>
19#include <string.h>
20#include <signal.h>
21#ifndef WINDOWS
22#include <sys/types.h>
23#include <unistd.h>
24#endif
25#ifdef CHROMIUM_THREADSAFE
26#include "cr_threads.h"
27#endif
28
29/**
30 * If you change this, see the comments in tilesortspu_context.c
31 */
32#define MAGIC_CONTEXT_BASE 500
33
34#define CONFIG_LOOKUP_FILE ".crconfigs"
35
36#ifdef WINDOWS
37#define PYTHON_EXE "python.exe"
38#else
39#define PYTHON_EXE "python"
40#endif
41
42#ifdef WINDOWS
43static char* gsViewportHackApps[] = {"googleearth.exe", NULL};
44#endif
45
46static int stub_initialized = 0;
47
48/* NOTE: 'SPUDispatchTable glim' is declared in NULLfuncs.py now */
49/* NOTE: 'SPUDispatchTable stubThreadsafeDispatch' is declared in tsfuncs.c */
50Stub stub;
51
52
53static void stubInitNativeDispatch( void )
54{
55#define MAX_FUNCS 1000
56 SPUNamedFunctionTable gl_funcs[MAX_FUNCS];
57 int numFuncs;
58
59 numFuncs = crLoadOpenGL( &stub.wsInterface, gl_funcs );
60
61 stub.haveNativeOpenGL = (numFuncs > 0);
62
63 /* XXX call this after context binding */
64 numFuncs += crLoadOpenGLExtensions( &stub.wsInterface, gl_funcs + numFuncs );
65
66 CRASSERT(numFuncs < MAX_FUNCS);
67
68 crSPUInitDispatchTable( &stub.nativeDispatch );
69 crSPUInitDispatch( &stub.nativeDispatch, gl_funcs );
70 crSPUInitDispatchNops( &stub.nativeDispatch );
71#undef MAX_FUNCS
72}
73
74
75/** Pointer to the SPU's real glClear and glViewport functions */
76static ClearFunc_t origClear;
77static ViewportFunc_t origViewport;
78static SwapBuffersFunc_t origSwapBuffers;
79static DrawBufferFunc_t origDrawBuffer;
80static ScissorFunc_t origScissor;
81
82static void stubCheckWindowState(void)
83{
84 WindowInfo *window;
85 bool bForceUpdate = false;
86
87 CRASSERT(stub.trackWindowSize || stub.trackWindowPos);
88
89 if (!stub.currentContext)
90 return;
91
92 window = stub.currentContext->currentDrawable;
93
94#ifdef WINDOWS
95 /* @todo install hook and track for WM_DISPLAYCHANGE */
96 {
97 DEVMODE devMode;
98
99 devMode.dmSize = sizeof(DEVMODE);
100 EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode);
101
102 if (devMode.dmPelsWidth!=window->dmPelsWidth || devMode.dmPelsHeight!=window->dmPelsHeight)
103 {
104 crDebug("Resolution changed(%d,%d), forcing window Pos/Size update", devMode.dmPelsWidth, devMode.dmPelsHeight);
105 window->dmPelsWidth = devMode.dmPelsWidth;
106 window->dmPelsHeight = devMode.dmPelsHeight;
107 bForceUpdate = true;
108 }
109 }
110#endif
111
112 stubUpdateWindowGeometry(window, bForceUpdate);
113
114#if defined(GLX) || defined (WINDOWS)
115 if (stub.trackWindowVisibleRgn)
116 {
117 stubUpdateWindowVisibileRegions(window);
118 }
119#endif
120
121 if (stub.trackWindowVisibility && window->type == CHROMIUM && window->drawable) {
122 const int mapped = stubIsWindowVisible(window);
123 if (mapped != window->mapped) {
124 crDebug("Dispatched: WindowShow(%i, %i)", window->spuWindow, mapped);
125 stub.spu->dispatch_table.WindowShow(window->spuWindow, mapped);
126 window->mapped = mapped;
127 }
128 }
129}
130
131
132/**
133 * Override the head SPU's glClear function.
134 * We're basically trapping this function so that we can poll the
135 * application window size at a regular interval.
136 */
137static void SPU_APIENTRY trapClear(GLbitfield mask)
138{
139 stubCheckWindowState();
140 /* call the original SPU glClear function */
141 origClear(mask);
142}
143
144/**
145 * As above, but for glViewport. Most apps call glViewport before
146 * glClear when a window is resized.
147 */
148static void SPU_APIENTRY trapViewport(GLint x, GLint y, GLsizei w, GLsizei h)
149{
150 stubCheckWindowState();
151 /* call the original SPU glViewport function */
152 if (!stub.viewportHack)
153 {
154 origViewport(x, y, w, h);
155 }
156 else
157 {
158 int winX, winY;
159 unsigned int winW, winH;
160 WindowInfo *pWindow;
161 pWindow = stub.currentContext->currentDrawable;
162 stubGetWindowGeometry(pWindow, &winX, &winY, &winW, &winH);
163 origViewport(0, 0, winW, winH);
164 }
165}
166
167static void SPU_APIENTRY trapSwapBuffers(GLint window, GLint flags)
168{
169 stubCheckWindowState();
170 origSwapBuffers(window, flags);
171}
172
173static void SPU_APIENTRY trapDrawBuffer(GLenum buf)
174{
175 stubCheckWindowState();
176 origDrawBuffer(buf);
177}
178
179static void SPU_APIENTRY trapScissor(GLint x, GLint y, GLsizei w, GLsizei h)
180{
181 int winX, winY;
182 unsigned int winW, winH;
183 WindowInfo *pWindow;
184 pWindow = stub.currentContext->currentDrawable;
185 stubGetWindowGeometry(pWindow, &winX, &winY, &winW, &winH);
186 origScissor(0, 0, winW, winH);
187}
188
189/**
190 * Use the GL function pointers in <spu> to initialize the static glim
191 * dispatch table.
192 */
193static void stubInitSPUDispatch(SPU *spu)
194{
195 crSPUInitDispatchTable( &stub.spuDispatch );
196 crSPUCopyDispatchTable( &stub.spuDispatch, &(spu->dispatch_table) );
197
198 if (stub.trackWindowSize || stub.trackWindowPos || stub.trackWindowVisibleRgn) {
199 /* patch-in special glClear/Viewport function to track window sizing */
200 origClear = stub.spuDispatch.Clear;
201 origViewport = stub.spuDispatch.Viewport;
202 origSwapBuffers = stub.spuDispatch.SwapBuffers;
203 origDrawBuffer = stub.spuDispatch.DrawBuffer;
204 origScissor = stub.spuDispatch.Scissor;
205 stub.spuDispatch.Clear = trapClear;
206 stub.spuDispatch.Viewport = trapViewport;
207 if (stub.viewportHack)
208 stub.spuDispatch.Scissor = trapScissor;
209 /*stub.spuDispatch.SwapBuffers = trapSwapBuffers;
210 stub.spuDispatch.DrawBuffer = trapDrawBuffer;*/
211 }
212
213 crSPUCopyDispatchTable( &glim, &stub.spuDispatch );
214}
215
216// Callback function, used to destroy all created contexts
217static void hsWalkStubDestroyContexts(unsigned long key, void *data1, void *data2)
218{
219 stubDestroyContext(key);
220}
221
222/**
223 * This is called when we exit.
224 * We call all the SPU's cleanup functions.
225 */
226static void stubSPUTearDown(void)
227{
228 crDebug("stubSPUTearDown");
229 if (!stub_initialized) return;
230
231 stub_initialized = 0;
232
233#ifdef WINDOWS
234 stubUninstallWindowMessageHook();
235#endif
236
237 //delete all created contexts
238 stubMakeCurrent( NULL, NULL);
239 crHashtableWalk(stub.contextTable, hsWalkStubDestroyContexts, NULL);
240
241 /* shutdown, now trap any calls to a NULL dispatcher */
242 crSPUCopyDispatchTable(&glim, &stubNULLDispatch);
243
244 crSPUUnloadChain(stub.spu);
245 stub.spu = NULL;
246
247#ifndef Linux
248 crUnloadOpenGL();
249#endif
250
251 crNetTearDown();
252
253#ifdef GLX
254 if (stub.xshmSI.shmid>=0)
255 {
256 shmctl(stub.xshmSI.shmid, IPC_RMID, 0);
257 shmdt(stub.xshmSI.shmaddr);
258 }
259#endif
260
261 crMemset(&stub, 0, sizeof(stub) );
262}
263
264static void stubSPUSafeTearDown(void)
265{
266#ifdef CHROMIUM_THREADSAFE
267 CRmutex *mutex;
268#endif
269
270 if (!stub_initialized) return;
271 stub_initialized = 0;
272
273#ifdef CHROMIUM_THREADSAFE
274 mutex = &stub.mutex;
275 crLockMutex(mutex);
276#endif
277 crDebug("stubSPUSafeTearDown");
278 crNetTearDown();
279#ifdef WINDOWS
280 stubUninstallWindowMessageHook();
281#endif
282 crMemset(&stub, 0, sizeof(stub));
283#ifdef CHROMIUM_THREADSAFE
284 crUnlockMutex(mutex);
285 crFreeMutex(mutex);
286#endif
287}
288
289
290static void stubExitHandler(void)
291{
292 stubSPUSafeTearDown();
293}
294
295/**
296 * Called when we receive a SIGTERM signal.
297 */
298static void stubSignalHandler(int signo)
299{
300 stubSPUSafeTearDown();
301 exit(0); /* this causes stubExitHandler() to be called */
302}
303
304
305/**
306 * Init variables in the stub structure, install signal handler.
307 */
308static void stubInitVars(void)
309{
310 WindowInfo *defaultWin;
311
312#ifdef CHROMIUM_THREADSAFE
313 crInitMutex(&stub.mutex);
314#endif
315
316 /* At the very least we want CR_RGB_BIT. */
317 stub.haveNativeOpenGL = GL_FALSE;
318 stub.spu = NULL;
319 stub.appDrawCursor = 0;
320 stub.minChromiumWindowWidth = 0;
321 stub.minChromiumWindowHeight = 0;
322 stub.maxChromiumWindowWidth = 0;
323 stub.maxChromiumWindowHeight = 0;
324 stub.matchChromiumWindowCount = 0;
325 stub.matchChromiumWindowID = NULL;
326 stub.matchWindowTitle = NULL;
327 stub.ignoreFreeglutMenus = 1;
328 stub.threadSafe = GL_FALSE;
329 stub.trackWindowSize = 0;
330 stub.trackWindowPos = 0;
331 stub.trackWindowVisibility = 0;
332 stub.trackWindowVisibleRgn = 0;
333 stub.mothershipPID = 0;
334 stub.spu_dir = NULL;
335
336 stub.freeContextNumber = MAGIC_CONTEXT_BASE;
337 stub.contextTable = crAllocHashtable();
338 stub.currentContext = NULL;
339
340 stub.windowTable = crAllocHashtable();
341
342 defaultWin = (WindowInfo *) crCalloc(sizeof(WindowInfo));
343 defaultWin->type = CHROMIUM;
344 defaultWin->spuWindow = 0; /* window 0 always exists */
345#ifdef WINDOWS
346 defaultWin->hVisibleRegion = INVALID_HANDLE_VALUE;
347#elif defined(GLX)
348 defaultWin->pVisibleRegions = NULL;
349 defaultWin->cVisibleRegions = 0;
350#endif
351 crHashtableAdd(stub.windowTable, 0, defaultWin);
352
353#if 1
354 atexit(stubExitHandler);
355 signal(SIGTERM, stubSignalHandler);
356 signal(SIGINT, stubSignalHandler);
357#ifndef WINDOWS
358 signal(SIGPIPE, SIG_IGN); /* the networking code should catch this */
359#endif
360#else
361 (void) stubExitHandler;
362 (void) stubSignalHandler;
363#endif
364}
365
366
367/**
368 * Return a free port number for the mothership to use, or -1 if we
369 * can't find one.
370 */
371static int
372GenerateMothershipPort(void)
373{
374 const int MAX_PORT = 10100;
375 unsigned short port;
376
377 /* generate initial port number randomly */
378 crRandAutoSeed();
379 port = (unsigned short) crRandInt(10001, MAX_PORT);
380
381#ifdef WINDOWS
382 /* XXX should implement a free port check here */
383 return port;
384#else
385 /*
386 * See if this port number really is free, try another if needed.
387 */
388 {
389 struct sockaddr_in servaddr;
390 int so_reuseaddr = 1;
391 int sock, k;
392
393 /* create socket */
394 sock = socket(AF_INET, SOCK_STREAM, 0);
395 CRASSERT(sock > 2);
396
397 /* deallocate socket/port when we exit */
398 k = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
399 (char *) &so_reuseaddr, sizeof(so_reuseaddr));
400 CRASSERT(k == 0);
401
402 /* initialize the servaddr struct */
403 crMemset(&servaddr, 0, sizeof(servaddr) );
404 servaddr.sin_family = AF_INET;
405 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
406
407 while (port < MAX_PORT) {
408 /* Bind to the given port number, return -1 if we fail */
409 servaddr.sin_port = htons((unsigned short) port);
410 k = bind(sock, (struct sockaddr *) &servaddr, sizeof(servaddr));
411 if (k) {
412 /* failed to create port. try next one. */
413 port++;
414 }
415 else {
416 /* free the socket/port now so mothership can make it */
417 close(sock);
418 return port;
419 }
420 }
421 }
422#endif /* WINDOWS */
423 return -1;
424}
425
426
427/**
428 * Try to determine which mothership configuration to use for this program.
429 */
430static char **
431LookupMothershipConfig(const char *procName)
432{
433 const int procNameLen = crStrlen(procName);
434 FILE *f;
435 const char *home;
436 char configPath[1000];
437
438 /* first, check if the CR_CONFIG env var is set */
439 {
440 const char *conf = crGetenv("CR_CONFIG");
441 if (conf && crStrlen(conf) > 0)
442 return crStrSplit(conf, " ");
443 }
444
445 /* second, look up config name from config file */
446 home = crGetenv("HOME");
447 if (home)
448 sprintf(configPath, "%s/%s", home, CONFIG_LOOKUP_FILE);
449 else
450 crStrcpy(configPath, CONFIG_LOOKUP_FILE); /* from current dir */
451 /* Check if the CR_CONFIG_PATH env var is set. */
452 {
453 const char *conf = crGetenv("CR_CONFIG_PATH");
454 if (conf)
455 crStrcpy(configPath, conf); /* from env var */
456 }
457
458 f = fopen(configPath, "r");
459 if (!f) {
460 return NULL;
461 }
462
463 while (!feof(f)) {
464 char line[1000];
465 char **args;
466 fgets(line, 999, f);
467 line[crStrlen(line) - 1] = 0; /* remove trailing newline */
468 if (crStrncmp(line, procName, procNameLen) == 0 &&
469 (line[procNameLen] == ' ' || line[procNameLen] == '\t'))
470 {
471 crWarning("Using Chromium configuration for %s from %s",
472 procName, configPath);
473 args = crStrSplit(line + procNameLen + 1, " ");
474 return args;
475 }
476 }
477 fclose(f);
478 return NULL;
479}
480
481
482static int Mothership_Awake = 0;
483
484
485/**
486 * Signal handler to determine when mothership is ready.
487 */
488static void
489MothershipPhoneHome(int signo)
490{
491 crDebug("Got signal %d: mothership is awake!", signo);
492 Mothership_Awake = 1;
493}
494
495void stubSetDefaultConfigurationOptions(void)
496{
497 unsigned char key[16]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
498
499 stub.appDrawCursor = 0;
500 stub.minChromiumWindowWidth = 0;
501 stub.minChromiumWindowHeight = 0;
502 stub.maxChromiumWindowWidth = 0;
503 stub.maxChromiumWindowHeight = 0;
504 stub.matchChromiumWindowID = NULL;
505 stub.numIgnoreWindowID = 0;
506 stub.matchWindowTitle = NULL;
507 stub.ignoreFreeglutMenus = 1;
508 stub.trackWindowSize = 1;
509 stub.trackWindowPos = 1;
510 stub.trackWindowVisibility = 1;
511 stub.trackWindowVisibleRgn = 1;
512 stub.matchChromiumWindowCount = 0;
513 stub.spu_dir = NULL;
514 crNetSetRank(0);
515 crNetSetContextRange(32, 35);
516 crNetSetNodeRange("iam0", "iamvis20");
517 crNetSetKey(key,sizeof(key));
518 stub.force_pbuffers = 0;
519 stub.viewportHack = 0;
520
521#ifdef WINDOWS
522 {
523 char name[1000];
524 int i;
525
526 crGetProcName(name, 1000);
527 for (i=0; gsViewportHackApps[i]; ++i)
528 {
529 if (!stricmp(name, gsViewportHackApps[i]))
530 {
531 stub.viewportHack = 1;
532 break;
533 }
534 }
535 }
536#endif
537}
538
539/**
540 * Do one-time initializations for the faker.
541 * Returns TRUE on success, FALSE otherwise.
542 */
543bool
544stubInit(void)
545{
546 /* Here is where we contact the mothership to find out what we're supposed
547 * to be doing. Networking code in a DLL initializer. I sure hope this
548 * works :)
549 *
550 * HOW can I pass the mothership address to this if I already know it?
551 */
552
553 CRConnection *conn = NULL;
554 char response[1024];
555 char **spuchain;
556 int num_spus;
557 int *spu_ids;
558 char **spu_names;
559 const char *app_id;
560 int i;
561
562 if (stub_initialized)
563 return true;
564
565 stubInitVars();
566
567 /* @todo check if it'd be of any use on other than guests, no use for windows */
568 app_id = crGetenv( "CR_APPLICATION_ID_NUMBER" );
569
570 crNetInit( NULL, NULL );
571
572#ifndef WINDOWS
573 {
574 CRNetServer ns;
575
576 ns.name = "vboxhgcm://host:0";
577 ns.buffer_size = 1024;
578 crNetServerConnect(&ns);
579 if (!ns.conn)
580 {
581 crWarning("Failed to connect to host. Make sure 3D acceleration is enabled for this VM.");
582 return false;
583 }
584 else
585 {
586 crNetFreeConnection(ns.conn);
587 }
588 }
589#endif
590
591 strcpy(response, "3 0 array 1 feedback 2 pack");
592 spuchain = crStrSplit( response, " " );
593 num_spus = crStrToInt( spuchain[0] );
594 spu_ids = (int *) crAlloc( num_spus * sizeof( *spu_ids ) );
595 spu_names = (char **) crAlloc( num_spus * sizeof( *spu_names ) );
596 for (i = 0 ; i < num_spus ; i++)
597 {
598 spu_ids[i] = crStrToInt( spuchain[2*i+1] );
599 spu_names[i] = crStrdup( spuchain[2*i+2] );
600 crDebug( "SPU %d/%d: (%d) \"%s\"", i+1, num_spus, spu_ids[i], spu_names[i] );
601 }
602
603 stubSetDefaultConfigurationOptions();
604
605 stub.spu = crSPULoadChain( num_spus, spu_ids, spu_names, stub.spu_dir, NULL );
606
607 crFree( spuchain );
608 crFree( spu_ids );
609 for (i = 0; i < num_spus; ++i)
610 crFree(spu_names[i]);
611 crFree( spu_names );
612
613 // spu chain load failed somewhere
614 if (!stub.spu) {
615 return false;
616 }
617
618 crSPUInitDispatchTable( &glim );
619
620 /* This is unlikely to change -- We still want to initialize our dispatch
621 * table with the functions of the first SPU in the chain. */
622 stubInitSPUDispatch( stub.spu );
623
624 /* we need to plug one special stub function into the dispatch table */
625 glim.GetChromiumParametervCR = stub_GetChromiumParametervCR;
626
627#if !defined(VBOX_NO_NATIVEGL)
628 /* Load pointers to native OpenGL functions into stub.nativeDispatch */
629 stubInitNativeDispatch();
630#endif
631
632/*crDebug("stub init");
633raise(SIGINT);*/
634
635#ifdef WINDOWS
636 stubInstallWindowMessageHook();
637#endif
638
639#ifdef GLX
640 stub.xshmSI.shmid = -1;
641 stub.bShmInitFailed = GL_FALSE;
642#endif
643
644 stub_initialized = 1;
645 return true;
646}
647
648
649
650/* Sigh -- we can't do initialization at load time, since Windows forbids
651 * the loading of other libraries from DLLMain. */
652
653#ifdef LINUX
654/* GCC crap
655 *void (*stub_init_ptr)(void) __attribute__((section(".ctors"))) = __stubInit; */
656#endif
657
658#ifdef WINDOWS
659#define WIN32_LEAN_AND_MEAN
660#include <windows.h>
661
662/* Windows crap */
663BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
664{
665 (void) lpvReserved;
666
667 switch (fdwReason)
668 {
669 case DLL_PROCESS_ATTACH:
670 {
671 CRNetServer ns;
672
673 crNetInit(NULL, NULL);
674 ns.name = "vboxhgcm://host:0";
675 ns.buffer_size = 1024;
676 crNetServerConnect(&ns);
677 if (!ns.conn)
678 {
679 crDebug("Failed to connect to host (is guest 3d acceleration enabled?), aborting ICD load.");
680 return FALSE;
681 }
682 else
683 crNetFreeConnection(ns.conn);
684
685 break;
686 }
687
688 case DLL_PROCESS_DETACH:
689 {
690 stubSPUSafeTearDown();
691 break;
692 }
693
694 case DLL_THREAD_ATTACH:
695 break;
696
697 case DLL_THREAD_DETACH:
698 break;
699
700 default:
701 break;
702 }
703
704 return TRUE;
705}
706#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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