VirtualBox

source: vbox/trunk/src/VBox/Additions/common/crOpenGL/context.c@ 40464

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

crOpenGL: context & window destroy fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 35.8 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/**
8 * \mainpage OpenGL_stub
9 *
10 * \section OpenGL_stubIntroduction Introduction
11 *
12 * Chromium consists of all the top-level files in the cr
13 * directory. The OpenGL_stub module basically takes care of API dispatch,
14 * and OpenGL state management.
15 *
16 */
17
18/**
19 * This file manages OpenGL rendering contexts in the faker library.
20 * The big issue is switching between Chromium and native GL context
21 * management. This is where we support multiple client OpenGL
22 * windows. Typically, one window is handled by Chromium while any
23 * other windows are handled by the native OpenGL library.
24 */
25
26#include "chromium.h"
27#include "cr_error.h"
28#include "cr_spu.h"
29#include "cr_mem.h"
30#include "cr_string.h"
31#include "cr_environment.h"
32#include "stub.h"
33
34/**
35 * This function should be called from MakeCurrent(). It'll detect if
36 * we're in a multi-thread situation, and do the right thing for dispatch.
37 */
38#ifdef CHROMIUM_THREADSAFE
39 static void
40stubCheckMultithread( void )
41{
42 static unsigned long knownID;
43 static GLboolean firstCall = GL_TRUE;
44
45 if (stub.threadSafe)
46 return; /* nothing new, nothing to do */
47
48 if (firstCall) {
49 knownID = crThreadID();
50 firstCall = GL_FALSE;
51 }
52 else if (knownID != crThreadID()) {
53 /* going thread-safe now! */
54 stub.threadSafe = GL_TRUE;
55 crSPUCopyDispatchTable(&glim, &stubThreadsafeDispatch);
56 }
57}
58#endif
59
60
61/**
62 * Install the given dispatch table as the table used for all gl* calls.
63 */
64 static void
65stubSetDispatch( SPUDispatchTable *table )
66{
67 CRASSERT(table);
68
69#ifdef CHROMIUM_THREADSAFE
70 /* always set the per-thread dispatch pointer */
71 crSetTSD(&stub.dispatchTSD, (void *) table);
72 if (stub.threadSafe) {
73 /* Do nothing - the thread-safe dispatch functions will call GetTSD()
74 * to get a pointer to the dispatch table, and jump through it.
75 */
76 }
77 else
78#endif
79 {
80 /* Single thread mode - just install the caller's dispatch table */
81 /* This conditional is an optimization to try to avoid unnecessary
82 * copying. It seems to work with atlantis, multiwin, etc. but
83 * _could_ be a problem. (Brian)
84 */
85 if (glim.copy_of != table->copy_of)
86 crSPUCopyDispatchTable(&glim, table);
87 }
88}
89
90
91/**
92 * Create a new _Chromium_ window, not GLX, WGL or CGL.
93 * Called by crWindowCreate() only.
94 */
95 GLint
96stubNewWindow( const char *dpyName, GLint visBits )
97{
98 WindowInfo *winInfo;
99 GLint spuWin, size[2];
100
101 spuWin = stub.spu->dispatch_table.WindowCreate( dpyName, visBits );
102 if (spuWin < 0) {
103 return -1;
104 }
105
106 winInfo = (WindowInfo *) crCalloc(sizeof(WindowInfo));
107 if (!winInfo) {
108 stub.spu->dispatch_table.WindowDestroy(spuWin);
109 return -1;
110 }
111
112 winInfo->type = CHROMIUM;
113
114 /* Ask the head SPU for the initial window size */
115 size[0] = size[1] = 0;
116 stub.spu->dispatch_table.GetChromiumParametervCR(GL_WINDOW_SIZE_CR, 0, GL_INT, 2, size);
117 if (size[0] == 0 && size[1] == 0) {
118 /* use some reasonable defaults */
119 size[0] = size[1] = 512;
120 }
121 winInfo->width = size[0];
122 winInfo->height = size[1];
123 winInfo->mapped = 1;
124
125 if (!dpyName)
126 dpyName = "";
127
128 crStrncpy(winInfo->dpyName, dpyName, MAX_DPY_NAME);
129 winInfo->dpyName[MAX_DPY_NAME-1] = 0;
130
131 /* Use spuWin as the hash table index and GLX/WGL handle */
132#ifdef WINDOWS
133 winInfo->drawable = (HDC) spuWin;
134 winInfo->hVisibleRegion = INVALID_HANDLE_VALUE;
135#elif defined(Darwin)
136 winInfo->drawable = (CGSWindowID) spuWin;
137#elif defined(GLX)
138 winInfo->drawable = (GLXDrawable) spuWin;
139 winInfo->pVisibleRegions = NULL;
140 winInfo->cVisibleRegions = 0;
141#endif
142#ifdef CR_NEWWINTRACK
143 winInfo->u32ClientID = stub.spu->dispatch_table.VBoxPackGetInjectID();
144#endif
145 winInfo->spuWindow = spuWin;
146
147 crHashtableAdd(stub.windowTable, (unsigned int) spuWin, winInfo);
148
149 return spuWin;
150}
151
152#ifdef GLX
153static XErrorHandler oldErrorHandler;
154static unsigned char lastXError = Success;
155
156static int
157errorHandler (Display *dpy, XErrorEvent *e)
158{
159 lastXError = e->error_code;
160 return 0;
161}
162#endif
163
164GLboolean
165stubIsWindowVisible(WindowInfo *win)
166{
167#if defined(WINDOWS)
168 return GL_TRUE;
169#elif defined(Darwin)
170 return GL_TRUE;
171#elif defined(GLX)
172 Display *dpy = stubGetWindowDisplay(win);
173 if (dpy)
174 {
175 XWindowAttributes attr;
176 XLOCK(dpy);
177 XGetWindowAttributes(dpy, win->drawable, &attr);
178 XUNLOCK(dpy);
179
180 if (attr.map_state == IsUnmapped)
181 {
182 return GL_FALSE;
183 }
184# if 1
185 return GL_TRUE;
186# else
187 if (attr.override_redirect)
188 {
189 return GL_TRUE;
190 }
191
192 if (!stub.bXExtensionsChecked)
193 {
194 stubCheckXExtensions(win);
195 }
196
197 if (!stub.bHaveXComposite)
198 {
199 return GL_TRUE;
200 }
201 else
202 {
203 Pixmap p;
204
205 crLockMutex(&stub.mutex);
206
207 XLOCK(dpy);
208 XSync(dpy, false);
209 oldErrorHandler = XSetErrorHandler(errorHandler);
210 /*@todo this will create new pixmap for window every call*/
211 p = XCompositeNameWindowPixmap(dpy, win->drawable);
212 XSync(dpy, false);
213 XSetErrorHandler(oldErrorHandler);
214 XUNLOCK(dpy);
215
216 switch (lastXError)
217 {
218 case Success:
219 XFreePixmap(dpy, p);
220 crUnlockMutex(&stub.mutex);
221 return GL_FALSE;
222 break;
223 case BadMatch:
224 /*Window isn't redirected*/
225 lastXError = Success;
226 break;
227 default:
228 crWarning("Unexpected XError %i", (int)lastXError);
229 lastXError = Success;
230 }
231
232 crUnlockMutex(&stub.mutex);
233
234 return GL_TRUE;
235 }
236# endif
237 }
238 else {
239 /* probably created by crWindowCreate() */
240 return win->mapped;
241 }
242#endif
243}
244
245
246/**
247 * Given a Windows HDC or GLX Drawable, return the corresponding
248 * WindowInfo structure. Create a new one if needed.
249 */
250WindowInfo *
251#ifdef WINDOWS
252 stubGetWindowInfo( HDC drawable )
253#elif defined(Darwin)
254 stubGetWindowInfo( CGSWindowID drawable )
255#elif defined(GLX)
256stubGetWindowInfo( Display *dpy, GLXDrawable drawable )
257#endif
258{
259#ifndef WINDOWS
260 WindowInfo *winInfo = (WindowInfo *) crHashtableSearch(stub.windowTable, (unsigned int) drawable);
261#else
262 WindowInfo *winInfo;
263 HWND hwnd;
264 hwnd = WindowFromDC(drawable);
265
266 if (!hwnd)
267 {
268 return NULL;
269 }
270
271 winInfo = (WindowInfo *) crHashtableSearch(stub.windowTable, (unsigned int) hwnd);
272#endif
273 if (!winInfo) {
274 winInfo = (WindowInfo *) crCalloc(sizeof(WindowInfo));
275 if (!winInfo)
276 return NULL;
277#ifdef GLX
278 crStrncpy(winInfo->dpyName, DisplayString(dpy), MAX_DPY_NAME);
279 winInfo->dpyName[MAX_DPY_NAME-1] = 0;
280 winInfo->dpy = dpy;
281 winInfo->pVisibleRegions = NULL;
282#elif defined(Darwin)
283 winInfo->connection = _CGSDefaultConnection(); // store our connection as default
284#elif defined(WINDOWS)
285 winInfo->hVisibleRegion = INVALID_HANDLE_VALUE;
286 winInfo->hWnd = hwnd;
287#endif
288 winInfo->drawable = drawable;
289 winInfo->type = UNDECIDED;
290 winInfo->spuWindow = -1;
291 winInfo->mapped = -1; /* don't know */
292 winInfo->pOwner = NULL;
293#ifdef CR_NEWWINTRACK
294 winInfo->u32ClientID = -1;
295#endif
296#ifndef WINDOWS
297 crHashtableAdd(stub.windowTable, (unsigned int) drawable, winInfo);
298#else
299 crHashtableAdd(stub.windowTable, (unsigned int) hwnd, winInfo);
300#endif
301 }
302#ifdef WINDOWS
303 else
304 {
305 winInfo->drawable = drawable;
306 }
307#endif
308 return winInfo;
309}
310
311static void stubWindowCheckOwnerCB(unsigned long key, void *data1, void *data2);
312
313static void
314stubDestroyContextLocked( ContextInfo *context )
315{
316 unsigned long contextId = context->id;
317 if (context->type == NATIVE) {
318#ifdef WINDOWS
319 stub.wsInterface.wglDeleteContext( context->hglrc );
320#elif defined(Darwin)
321 stub.wsInterface.CGLDestroyContext( context->cglc );
322#elif defined(GLX)
323 stub.wsInterface.glXDestroyContext( context->dpy, context->glxContext );
324#endif
325 }
326 else if (context->type == CHROMIUM) {
327 /* Have pack SPU or tilesort SPU, etc. destroy the context */
328 CRASSERT(context->spuContext >= 0);
329 stub.spu->dispatch_table.DestroyContext( context->spuContext );
330 crHashtableWalk(stub.windowTable, stubWindowCheckOwnerCB, context);
331 }
332
333#ifdef GLX
334 crFreeHashtable(context->pGLXPixmapsHash, crFree);
335 if (context->damageDpy)
336 {
337 XCloseDisplay(context->damageDpy);
338 }
339#endif
340
341 crMemZero(context, sizeof(ContextInfo)); /* just to be safe */
342 crHashtableDelete(stub.contextTable, contextId, crFree);
343}
344
345#ifdef CHROMIUM_THREADSAFE
346static DECLCALLBACK(void) stubContextDtor(void*pvContext)
347{
348 crHashtableLock(stub.windowTable);
349 crHashtableLock(stub.contextTable);
350 stubDestroyContextLocked((ContextInfo*)pvContext);
351 crHashtableUnlock(stub.contextTable);
352 crHashtableUnlock(stub.windowTable);
353}
354#endif
355
356/**
357 * Allocate a new ContextInfo object, initialize it, put it into the
358 * context hash table. If type==CHROMIUM, call the head SPU's
359 * CreateContext() function too.
360 */
361 ContextInfo *
362stubNewContext( const char *dpyName, GLint visBits, ContextType type,
363 unsigned long shareCtx )
364{
365 GLint spuContext = -1, spuShareCtx = 0;
366 ContextInfo *context;
367
368 if (shareCtx > 0) {
369 /* translate shareCtx to a SPU context ID */
370 context = (ContextInfo *)
371 crHashtableSearch(stub.contextTable, shareCtx);
372 if (context)
373 spuShareCtx = context->spuContext;
374 }
375
376 if (type == CHROMIUM) {
377 spuContext
378 = stub.spu->dispatch_table.CreateContext(dpyName, visBits, spuShareCtx);
379 if (spuContext < 0)
380 return NULL;
381 }
382
383 context = crCalloc(sizeof(ContextInfo));
384 if (!context) {
385 stub.spu->dispatch_table.DestroyContext(spuContext);
386 return NULL;
387 }
388
389 if (!dpyName)
390 dpyName = "";
391
392 context->id = stub.freeContextNumber++;
393 context->type = type;
394 context->spuContext = spuContext;
395 context->visBits = visBits;
396 context->currentDrawable = NULL;
397 crStrncpy(context->dpyName, dpyName, MAX_DPY_NAME);
398 context->dpyName[MAX_DPY_NAME-1] = 0;
399
400#ifdef CHROMIUM_THREADSAFE
401 VBoxTlsRefInit(context, stubContextDtor);
402#endif
403
404#if defined(GLX) || defined(DARWIN)
405 context->share = (ContextInfo *)
406 crHashtableSearch(stub.contextTable, (unsigned long) shareCtx);
407#endif
408
409#ifdef GLX
410 context->pGLXPixmapsHash = crAllocHashtable();
411 context->damageInitFailed = GL_FALSE;
412 context->damageDpy = NULL;
413 context->damageEventsBase = 0;
414#endif
415
416 crHashtableAdd(stub.contextTable, context->id, (void *) context);
417
418 return context;
419}
420
421
422#ifdef Darwin
423
424#define SET_ATTR(l,i,a) ( (l)[(i)++] = (a) )
425#define SET_ATTR_V(l,i,a,v) ( SET_ATTR(l,i,a), SET_ATTR(l,i,v) )
426
427void stubSetPFA( ContextInfo *ctx, CGLPixelFormatAttribute *attribs, int size, GLint *num ) {
428 GLuint visual = ctx->visBits;
429 int i = 0;
430
431 CRASSERT(visual & CR_RGB_BIT);
432
433 SET_ATTR_V(attribs, i, kCGLPFAColorSize, 8);
434
435 if( visual & CR_DEPTH_BIT )
436 SET_ATTR_V(attribs, i, kCGLPFADepthSize, 16);
437
438 if( visual & CR_ACCUM_BIT )
439 SET_ATTR_V(attribs, i, kCGLPFAAccumSize, 1);
440
441 if( visual & CR_STENCIL_BIT )
442 SET_ATTR_V(attribs, i, kCGLPFAStencilSize, 1);
443
444 if( visual & CR_ALPHA_BIT )
445 SET_ATTR_V(attribs, i, kCGLPFAAlphaSize, 1);
446
447 if( visual & CR_DOUBLE_BIT )
448 SET_ATTR(attribs, i, kCGLPFADoubleBuffer);
449
450 if( visual & CR_STEREO_BIT )
451 SET_ATTR(attribs, i, kCGLPFAStereo);
452
453/* SET_ATTR_V(attribs, i, kCGLPFASampleBuffers, 1);
454 SET_ATTR_V(attribs, i, kCGLPFASamples, 0);
455 SET_ATTR_V(attribs, i, kCGLPFADisplayMask, 0); */
456 SET_ATTR(attribs, i, kCGLPFABackingStore);
457 SET_ATTR(attribs, i, kCGLPFAWindow);
458 SET_ATTR_V(attribs, i, kCGLPFADisplayMask, ctx->disp_mask);
459
460 SET_ATTR(attribs, i, 0);
461
462 *num = i;
463}
464
465#endif
466
467/**
468 * This creates a native GLX/WGL context.
469 */
470static GLboolean
471InstantiateNativeContext( WindowInfo *window, ContextInfo *context )
472{
473#ifdef WINDOWS
474 context->hglrc = stub.wsInterface.wglCreateContext( window->drawable );
475 return context->hglrc ? GL_TRUE : GL_FALSE;
476#elif defined(Darwin)
477 CGLContextObj shareCtx = NULL;
478 CGLPixelFormatObj pix;
479 long npix;
480
481 CGLPixelFormatAttribute attribs[16];
482 GLint ind = 0;
483
484 if( context->share ) {
485 if( context->cglc != context->share->cglc ) {
486 crWarning("CGLCreateContext() is trying to share a non-existant "
487 "CGL context. Setting share context to zero.");
488 shareCtx = 0;
489 }
490 else
491 shareCtx = context->cglc;
492 }
493
494 stubSetPFA( context, attribs, 16, &ind );
495
496 stub.wsInterface.CGLChoosePixelFormat( attribs, &pix, &npix );
497 stub.wsInterface.CGLCreateContext( pix, shareCtx, &context->cglc );
498 if( !context->cglc )
499 crError("InstantiateNativeContext: Couldn't Create the context!");
500
501 stub.wsInterface.CGLDestroyPixelFormat( pix );
502
503 if( context->parambits ) {
504 /* Set the delayed parameters */
505 if( context->parambits & VISBIT_SWAP_RECT )
506 stub.wsInterface.CGLSetParameter( context->cglc, kCGLCPSwapRectangle, context->swap_rect );
507
508 if( context->parambits & VISBIT_SWAP_INTERVAL )
509 stub.wsInterface.CGLSetParameter( context->cglc, kCGLCPSwapInterval, &(context->swap_interval) );
510
511 if( context->parambits & VISBIT_CLIENT_STORAGE )
512 stub.wsInterface.CGLSetParameter( context->cglc, kCGLCPClientStorage, (long*)&(context->client_storage) );
513
514 context->parambits = 0;
515 }
516
517 return context->cglc ? GL_TRUE : GL_FALSE;
518#elif defined(GLX)
519 GLXContext shareCtx = 0;
520
521 /* sort out context sharing here */
522 if (context->share) {
523 if (context->glxContext != context->share->glxContext) {
524 crWarning("glXCreateContext() is trying to share a non-existant "
525 "GLX context. Setting share context to zero.");
526 shareCtx = 0;
527 }
528 else {
529 shareCtx = context->glxContext;
530 }
531 }
532
533 context->glxContext = stub.wsInterface.glXCreateContext( window->dpy,
534 context->visual, shareCtx, context->direct );
535
536 return context->glxContext ? GL_TRUE : GL_FALSE;
537#endif
538}
539
540
541/**
542 * Utility functions to get window size and titlebar text.
543 */
544#ifdef WINDOWS
545
546void
547stubGetWindowGeometry(const WindowInfo *window, int *x, int *y,
548 unsigned int *w, unsigned int *h )
549{
550 RECT rect;
551
552 if (!window->drawable || !window->hWnd) {
553 *w = *h = 0;
554 return;
555 }
556
557 if (window->hWnd!=WindowFromDC(window->drawable))
558 {
559 crWarning("Window(%i) DC is no longer valid", window->spuWindow);
560 return;
561 }
562
563 if (!GetClientRect(window->hWnd, &rect))
564 {
565 crWarning("GetClientRect failed for %p", window->hWnd);
566 *w = *h = 0;
567 return;
568 }
569 *w = rect.right - rect.left;
570 *h = rect.bottom - rect.top;
571
572 if (!ClientToScreen( window->hWnd, (LPPOINT) &rect ))
573 {
574 crWarning("ClientToScreen failed for %p", window->hWnd);
575 *w = *h = 0;
576 return;
577 }
578 *x = rect.left;
579 *y = rect.top;
580}
581
582static void
583GetWindowTitle( const WindowInfo *window, char *title )
584{
585 /* XXX - we don't handle recurseUp */
586 if (window->hWnd)
587 GetWindowText(window->hWnd, title, 100);
588 else
589 title[0] = 0;
590}
591
592static void
593GetCursorPosition(WindowInfo *window, int pos[2])
594{
595 RECT rect;
596 POINT point;
597 GLint size[2], x, y;
598 unsigned int NativeHeight, NativeWidth, ChromiumHeight, ChromiumWidth;
599 float WidthRatio, HeightRatio;
600 static int DebugFlag = 0;
601
602 // apparently the "window" parameter passed to this
603 // function contains the native window information
604 HWND NATIVEhwnd = window->hWnd;
605
606 if (NATIVEhwnd!=WindowFromDC(window->drawable))
607 {
608 crWarning("Window(%i) DC is no longer valid", window->spuWindow);
609 return;
610 }
611
612 // get the native window's height and width
613 stubGetWindowGeometry(window, &x, &y, &NativeWidth, &NativeHeight);
614
615 // get the spu window's height and width
616 stub.spu->dispatch_table.GetChromiumParametervCR(GL_WINDOW_SIZE_CR, window->spuWindow, GL_INT, 2, size);
617 ChromiumWidth = size[0];
618 ChromiumHeight = size[1];
619
620 // get the ratio of the size of the native window to the cr window
621 WidthRatio = (float)ChromiumWidth / (float)NativeWidth;
622 HeightRatio = (float)ChromiumHeight / (float)NativeHeight;
623
624 // output some debug information at the beginning
625 if(DebugFlag)
626 {
627 DebugFlag = 0;
628 crDebug("Native Window Handle = %d", NATIVEhwnd);
629 crDebug("Native Width = %i", NativeWidth);
630 crDebug("Native Height = %i", NativeHeight);
631 crDebug("Chromium Width = %i", ChromiumWidth);
632 crDebug("Chromium Height = %i", ChromiumHeight);
633 }
634
635 if (NATIVEhwnd)
636 {
637 GetClientRect( NATIVEhwnd, &rect );
638 GetCursorPos (&point);
639
640 // make sure these coordinates are relative to the native window,
641 // not the whole desktop
642 ScreenToClient(NATIVEhwnd, &point);
643
644 // calculate the new position of the virtual cursor
645 pos[0] = (int)(point.x * WidthRatio);
646 pos[1] = (int)((NativeHeight - point.y) * HeightRatio);
647 }
648 else
649 {
650 pos[0] = 0;
651 pos[1] = 0;
652 }
653}
654
655#elif defined(Darwin)
656
657extern OSStatus CGSGetScreenRectForWindow( CGSConnectionID cid, CGSWindowID wid, float *outRect );
658extern OSStatus CGSGetWindowBounds( CGSConnectionID cid, CGSWindowID wid, float *bounds );
659
660void
661stubGetWindowGeometry( const WindowInfo *window, int *x, int *y, unsigned int *w, unsigned int *h )
662{
663 float rect[4];
664
665 if( !window ||
666 !window->connection ||
667 !window->drawable ||
668 CGSGetWindowBounds( window->connection, window->drawable, rect ) != noErr )
669 {
670 *x = *y = 0;
671 *w = *h = 0;
672 } else {
673 *x = (int) rect[0];
674 *y = (int) rect[1];
675 *w = (int) rect[2];
676 *h = (int) rect[3];
677 }
678}
679
680
681static void
682GetWindowTitle( const WindowInfo *window, char *title )
683{
684 /* XXX \todo Darwin window Title */
685 title[0] = '\0';
686}
687
688
689static void
690GetCursorPosition( const WindowInfo *window, int pos[2] )
691{
692 Point mouse_pos;
693 float window_rect[4];
694
695 GetMouse( &mouse_pos );
696 CGSGetScreenRectForWindow( window->connection, window->drawable, window_rect );
697
698 pos[0] = mouse_pos.h - (int) window_rect[0];
699 pos[1] = (int) window_rect[3] - (mouse_pos.v - (int) window_rect[1]);
700
701 /*crDebug( "%i %i", pos[0], pos[1] );*/
702}
703
704#elif defined(GLX)
705
706void
707stubGetWindowGeometry(WindowInfo *window, int *x, int *y, unsigned int *w, unsigned int *h)
708{
709 Window root, child;
710 unsigned int border, depth;
711 Display *dpy;
712
713 dpy = stubGetWindowDisplay(window);
714
715 //@todo: Performing those checks is expensive operation, especially for simple apps with high FPS.
716 // Disabling those triples glxgears fps, thus using xevents instead of per frame polling is much more preferred.
717 //@todo: Check similar on windows guests, though doubtful as there're no XSync like calls on windows.
718 if (window && dpy)
719 {
720 XLOCK(dpy);
721 }
722
723 if (!window
724 || !dpy
725 || !window->drawable
726 || !XGetGeometry(dpy, window->drawable, &root, x, y, w, h, &border, &depth)
727 || !XTranslateCoordinates(dpy, window->drawable, root, 0, 0, x, y, &child))
728 {
729 crWarning("Failed to get windows geometry for %p, try xwininfo", window);
730 *x = *y = 0;
731 *w = *h = 0;
732 }
733
734 if (window && dpy)
735 {
736 XUNLOCK(dpy);
737 }
738}
739
740static char *
741GetWindowTitleHelper( Display *dpy, Window window, GLboolean recurseUp )
742{
743 while (1) {
744 char *name;
745 if (!XFetchName(dpy, window, &name))
746 return NULL;
747 if (name[0]) {
748 return name;
749 }
750 else if (recurseUp) {
751 /* This window has no name, try the parent */
752 Status stat;
753 Window root, parent, *children;
754 unsigned int numChildren;
755 stat = XQueryTree( dpy, window, &root, &parent,
756 &children, &numChildren );
757 if (!stat || window == root)
758 return NULL;
759 if (children)
760 XFree(children);
761 window = parent;
762 }
763 else {
764 XFree(name);
765 return NULL;
766 }
767 }
768}
769
770static void
771GetWindowTitle( const WindowInfo *window, char *title )
772{
773 char *t = GetWindowTitleHelper(window->dpy, window->drawable, GL_TRUE);
774 if (t) {
775 crStrcpy(title, t);
776 XFree(t);
777 }
778 else {
779 title[0] = 0;
780 }
781}
782
783
784/**
785 *Return current cursor position in local window coords.
786 */
787static void
788GetCursorPosition(WindowInfo *window, int pos[2] )
789{
790 int rootX, rootY;
791 Window root, child;
792 unsigned int mask;
793 int x, y;
794
795 XLOCK(window->dpy);
796
797 Bool q = XQueryPointer(window->dpy, window->drawable, &root, &child,
798 &rootX, &rootY, &pos[0], &pos[1], &mask);
799 if (q) {
800 unsigned int w, h;
801 stubGetWindowGeometry( window, &x, &y, &w, &h );
802 /* invert Y */
803 pos[1] = (int) h - pos[1] - 1;
804 }
805 else {
806 pos[0] = pos[1] = 0;
807 }
808
809 XUNLOCK(window->dpy);
810}
811
812#endif
813
814
815/**
816 * This function is called by MakeCurrent() and determines whether or
817 * not a new rendering context should be bound to Chromium or the native
818 * OpenGL.
819 * \return GL_FALSE if native OpenGL should be used, or GL_TRUE if Chromium
820 * should be used.
821 */
822static GLboolean
823stubCheckUseChromium( WindowInfo *window )
824{
825 int x, y;
826 unsigned int w, h;
827
828 /* If the provided window is CHROMIUM, we're clearly intended
829 * to create a CHROMIUM context.
830 */
831 if (window->type == CHROMIUM)
832 return GL_TRUE;
833
834 if (stub.ignoreFreeglutMenus) {
835 const char *glutMenuTitle = "freeglut menu";
836 char title[1000];
837 GetWindowTitle(window, title);
838 if (crStrcmp(title, glutMenuTitle) == 0) {
839 crDebug("GL faker: Ignoring freeglut menu window");
840 return GL_FALSE;
841 }
842 }
843
844 /* If the user's specified a window count for Chromium, see if
845 * this window satisfies that criterium.
846 */
847 stub.matchChromiumWindowCounter++;
848 if (stub.matchChromiumWindowCount > 0) {
849 if (stub.matchChromiumWindowCounter != stub.matchChromiumWindowCount) {
850 crDebug("Using native GL, app window doesn't meet match_window_count");
851 return GL_FALSE;
852 }
853 }
854
855 /* If the user's specified a window list to ignore, see if this
856 * window satisfies that criterium.
857 */
858 if (stub.matchChromiumWindowID) {
859 GLuint i;
860
861 for (i = 0; i <= stub.numIgnoreWindowID; i++) {
862 if (stub.matchChromiumWindowID[i] == stub.matchChromiumWindowCounter) {
863 crDebug("Ignore window ID %d, using native GL", stub.matchChromiumWindowID[i]);
864 return GL_FALSE;
865 }
866 }
867 }
868
869 /* If the user's specified a minimum window size for Chromium, see if
870 * this window satisfies that criterium.
871 */
872 if (stub.minChromiumWindowWidth > 0 &&
873 stub.minChromiumWindowHeight > 0) {
874 stubGetWindowGeometry( window, &x, &y, &w, &h );
875 if (w >= stub.minChromiumWindowWidth &&
876 h >= stub.minChromiumWindowHeight) {
877
878 /* Check for maximum sized window now too */
879 if (stub.maxChromiumWindowWidth &&
880 stub.maxChromiumWindowHeight) {
881 if (w < stub.maxChromiumWindowWidth &&
882 h < stub.maxChromiumWindowHeight)
883 return GL_TRUE;
884 else
885 return GL_FALSE;
886 }
887
888 return GL_TRUE;
889 }
890 crDebug("Using native GL, app window doesn't meet minimum_window_size");
891 return GL_FALSE;
892 }
893 else if (stub.matchWindowTitle) {
894 /* If the user's specified a window title for Chromium, see if this
895 * window satisfies that criterium.
896 */
897 GLboolean wildcard = GL_FALSE;
898 char title[1000];
899 char *titlePattern;
900 int len;
901 /* check for leading '*' wildcard */
902 if (stub.matchWindowTitle[0] == '*') {
903 titlePattern = crStrdup( stub.matchWindowTitle + 1 );
904 wildcard = GL_TRUE;
905 }
906 else {
907 titlePattern = crStrdup( stub.matchWindowTitle );
908 }
909 /* check for trailing '*' wildcard */
910 len = crStrlen(titlePattern);
911 if (len > 0 && titlePattern[len - 1] == '*') {
912 titlePattern[len - 1] = '\0'; /* terminate here */
913 wildcard = GL_TRUE;
914 }
915
916 GetWindowTitle( window, title );
917 if (title[0]) {
918 if (wildcard) {
919 if (crStrstr(title, titlePattern)) {
920 crFree(titlePattern);
921 return GL_TRUE;
922 }
923 }
924 else if (crStrcmp(title, titlePattern) == 0) {
925 crFree(titlePattern);
926 return GL_TRUE;
927 }
928 }
929 crFree(titlePattern);
930 crDebug("Using native GL, app window title doesn't match match_window_title string (\"%s\" != \"%s\")", title, stub.matchWindowTitle);
931 return GL_FALSE;
932 }
933
934 /* Window title and size don't matter */
935 CRASSERT(stub.minChromiumWindowWidth == 0);
936 CRASSERT(stub.minChromiumWindowHeight == 0);
937 CRASSERT(stub.matchWindowTitle == NULL);
938
939 /* User hasn't specified a width/height or window title.
940 * We'll use chromium for this window (and context) if no other is.
941 */
942
943 return GL_TRUE; /* use Chromium! */
944}
945
946static void stubWindowCheckOwnerCB(unsigned long key, void *data1, void *data2)
947{
948 WindowInfo *pWindow = (WindowInfo *) data1;
949 ContextInfo *pCtx = (ContextInfo *) data2;
950
951 if (pWindow->pOwner == pCtx)
952 {
953#ifdef WINDOWS
954 /* Note: can't use WindowFromDC(context->pOwnWindow->drawable) here
955 because GL context is already released from DC and actual guest window
956 could be destroyed.
957 */
958 crWindowDestroy((GLint)pWindow->hWnd);
959#else
960 crWindowDestroy((GLint)pWindow->drawable);
961#endif
962 }
963}
964
965GLboolean
966stubMakeCurrent( WindowInfo *window, ContextInfo *context )
967{
968 GLboolean retVal;
969
970 /*
971 * Get WindowInfo and ContextInfo pointers.
972 */
973
974 if (!context || !window) {
975 ContextInfo * currentContext = stubGetCurrentContext();
976 if (currentContext)
977 currentContext->currentDrawable = NULL;
978 if (context)
979 context->currentDrawable = NULL;
980 stubSetCurrentContext(NULL);
981 return GL_TRUE; /* OK */
982 }
983
984#ifdef CHROMIUM_THREADSAFE
985 stubCheckMultithread();
986#endif
987
988 if (context->type == UNDECIDED) {
989 /* Here's where we really create contexts */
990#ifdef CHROMIUM_THREADSAFE
991 crLockMutex(&stub.mutex);
992#endif
993
994 if (stubCheckUseChromium(window)) {
995 /*
996 * Create a Chromium context.
997 */
998#if defined(GLX) || defined(DARWIN)
999 GLint spuShareCtx = context->share ? context->share->spuContext : 0;
1000#else
1001 GLint spuShareCtx = 0;
1002#endif
1003
1004 CRASSERT(stub.spu);
1005 CRASSERT(stub.spu->dispatch_table.CreateContext);
1006 context->type = CHROMIUM;
1007
1008 context->spuContext
1009 = stub.spu->dispatch_table.CreateContext( context->dpyName,
1010 context->visBits,
1011 spuShareCtx );
1012 if (window->spuWindow == -1)
1013 {
1014 /*crDebug("(1)stubMakeCurrent ctx=%p(%i) window=%p(%i)", context, context->spuContext, window, window->spuWindow);*/
1015 window->spuWindow = stub.spu->dispatch_table.WindowCreate( window->dpyName, context->visBits );
1016#ifdef CR_NEWWINTRACK
1017 window->u32ClientID = stub.spu->dispatch_table.VBoxPackGetInjectID();
1018#endif
1019 }
1020 }
1021 else {
1022 /*
1023 * Create a native OpenGL context.
1024 */
1025 if (!InstantiateNativeContext(window, context))
1026 {
1027#ifdef CHROMIUM_THREADSAFE
1028 crUnlockMutex(&stub.mutex);
1029#endif
1030 return 0; /* false */
1031 }
1032 context->type = NATIVE;
1033 }
1034
1035#ifdef CHROMIUM_THREADSAFE
1036 crUnlockMutex(&stub.mutex);
1037#endif
1038 }
1039
1040
1041 if (context->type == NATIVE) {
1042 /*
1043 * Native OpenGL MakeCurrent().
1044 */
1045#ifdef WINDOWS
1046 retVal = (GLboolean) stub.wsInterface.wglMakeCurrent( window->drawable, context->hglrc );
1047#elif defined(Darwin)
1048 // XXX \todo We need to differentiate between these two..
1049 retVal = ( stub.wsInterface.CGLSetSurface(context->cglc, window->connection, window->drawable, window->surface) == noErr );
1050 retVal = ( stub.wsInterface.CGLSetCurrentContext(context->cglc) == noErr );
1051#elif defined(GLX)
1052 retVal = (GLboolean) stub.wsInterface.glXMakeCurrent( window->dpy, window->drawable, context->glxContext );
1053#endif
1054 }
1055 else {
1056 /*
1057 * SPU chain MakeCurrent().
1058 */
1059 CRASSERT(context->type == CHROMIUM);
1060 CRASSERT(context->spuContext >= 0);
1061
1062 /*if (context->currentDrawable && context->currentDrawable != window)
1063 crDebug("Rebinding context %p to a different window", context);*/
1064
1065 if (window->type == NATIVE) {
1066 crWarning("Can't rebind a chromium context to a native window\n");
1067 retVal = 0;
1068 }
1069 else {
1070 if (window->spuWindow == -1)
1071 {
1072 /*crDebug("(2)stubMakeCurrent ctx=%p(%i) window=%p(%i)", context, context->spuContext, window, window->spuWindow);*/
1073 window->spuWindow = stub.spu->dispatch_table.WindowCreate( window->dpyName, context->visBits );
1074#ifdef CR_NEWWINTRACK
1075 window->u32ClientID = stub.spu->dispatch_table.VBoxPackGetInjectID();
1076#endif
1077 if (context->currentDrawable && context->currentDrawable->type==CHROMIUM
1078 && context->currentDrawable->pOwner==context)
1079 {
1080#ifdef WINDOWS
1081 if (context->currentDrawable->hWnd!=WindowFromDC(context->currentDrawable->drawable))
1082 {
1083 crWindowDestroy((GLint)context->currentDrawable->hWnd);
1084 }
1085#else
1086 Window root;
1087 int x, y;
1088 unsigned int border, depth, w, h;
1089
1090 XLOCK(context->currentDrawable->dpy);
1091 if (!XGetGeometry(context->currentDrawable->dpy, context->currentDrawable->drawable, &root, &x, &y, &w, &h, &border, &depth))
1092 {
1093 crWindowDestroy((GLint)context->currentDrawable->drawable);
1094 }
1095 XUNLOCK(context->currentDrawable->dpy);
1096#endif
1097
1098 }
1099 }
1100
1101 if (window->spuWindow != (GLint)window->drawable)
1102 stub.spu->dispatch_table.MakeCurrent( window->spuWindow, (GLint) window->drawable, context->spuContext );
1103 else
1104 stub.spu->dispatch_table.MakeCurrent( window->spuWindow, 0, /* native window handle */ context->spuContext );
1105
1106 retVal = 1;
1107 }
1108 }
1109
1110 window->type = context->type;
1111 window->pOwner = context;
1112 context->currentDrawable = window;
1113 stubSetCurrentContext(context);
1114
1115 if (retVal) {
1116 /* Now, if we've transitions from Chromium to native rendering, or
1117 * vice versa, we have to change all the OpenGL entrypoint pointers.
1118 */
1119 if (context->type == NATIVE) {
1120 /* Switch to native API */
1121 /*printf(" Switching to native API\n");*/
1122 stubSetDispatch(&stub.nativeDispatch);
1123 }
1124 else if (context->type == CHROMIUM) {
1125 /* Switch to stub (SPU) API */
1126 /*printf(" Switching to spu API\n");*/
1127 stubSetDispatch(&stub.spuDispatch);
1128 }
1129 else {
1130 /* no API switch needed */
1131 }
1132 }
1133
1134 if (!window->width && window->type == CHROMIUM) {
1135 /* One time window setup */
1136 int x, y;
1137 unsigned int winW, winH;
1138
1139 stubGetWindowGeometry( window, &x, &y, &winW, &winH );
1140
1141 /* If we're not using GLX/WGL (no app window) we'll always get
1142 * a width and height of zero here. In that case, skip the viewport
1143 * call since we're probably using a tilesort SPU with fake_window_dims
1144 * which the tilesort SPU will use for the viewport.
1145 */
1146 window->width = winW;
1147 window->height = winH;
1148 if (stub.trackWindowSize)
1149 stub.spuDispatch.WindowSize( window->spuWindow, winW, winH );
1150 if (stub.trackWindowPos)
1151 stub.spuDispatch.WindowPosition(window->spuWindow, x, y);
1152 if (winW > 0 && winH > 0)
1153 stub.spu->dispatch_table.Viewport( 0, 0, winW, winH );
1154#ifdef VBOX_WITH_WDDM
1155 stub.spu->dispatch_table.WindowVisibleRegion(window->spuWindow, 0, NULL);
1156#endif
1157 }
1158
1159 /* Update window mapping state.
1160 * Basically, this lets us hide render SPU windows which correspond
1161 * to unmapped application windows. Without this, "pertly" (for example)
1162 * opens *lots* of temporary windows which otherwise clutter the screen.
1163 */
1164 if (stub.trackWindowVisibility && window->type == CHROMIUM && window->drawable) {
1165 const int mapped = stubIsWindowVisible(window);
1166 if (mapped != window->mapped) {
1167 crDebug("Dispatched: WindowShow(%i, %i)", window->spuWindow, mapped);
1168 stub.spu->dispatch_table.WindowShow(window->spuWindow, mapped);
1169 window->mapped = mapped;
1170 }
1171 }
1172
1173 return retVal;
1174}
1175
1176void
1177stubDestroyContext( unsigned long contextId )
1178{
1179 ContextInfo *context;
1180
1181 if (!stub.contextTable) {
1182 return;
1183 }
1184
1185 /* the lock order is windowTable->contextTable (see wglMakeCurrent_prox, glXMakeCurrent)
1186 * this is why we need to take a windowTable lock since we will later do stub.windowTable access & locking */
1187 crHashtableLock(stub.windowTable);
1188 crHashtableLock(stub.contextTable);
1189
1190 context = (ContextInfo *) crHashtableSearch(stub.contextTable, contextId);
1191
1192 CRASSERT(context);
1193
1194#ifdef CHROMIUM_THREADSAFE
1195 if (stubGetCurrentContext() == context) {
1196 stubSetCurrentContext(NULL);
1197 }
1198
1199 VBoxTlsRefRelease(context);
1200#else
1201 stubDestroyContextLocked(context);
1202
1203 if (stubGetCurrentContext() == context) {
1204 stubSetCurrentContext(NULL);
1205 }
1206#endif
1207 crHashtableUnlock(stub.contextTable);
1208 crHashtableUnlock(stub.windowTable);
1209}
1210
1211void
1212stubSwapBuffers(WindowInfo *window, GLint flags)
1213{
1214 if (!window)
1215 return;
1216
1217 /* Determine if this window is being rendered natively or through
1218 * Chromium.
1219 */
1220
1221 if (window->type == NATIVE) {
1222 /*printf("*** Swapping native window %d\n", (int) drawable);*/
1223#ifdef WINDOWS
1224 (void) stub.wsInterface.wglSwapBuffers( window->drawable );
1225#elif defined(Darwin)
1226 /* ...is this ok? */
1227/* stub.wsInterface.CGLFlushDrawable( context->cglc ); */
1228 crDebug("stubSwapBuffers: unable to swap (no context!)");
1229#elif defined(GLX)
1230 stub.wsInterface.glXSwapBuffers( window->dpy, window->drawable );
1231#endif
1232 }
1233 else if (window->type == CHROMIUM) {
1234 /* Let the SPU do the buffer swap */
1235 /*printf("*** Swapping chromium window %d\n", (int) drawable);*/
1236 if (stub.appDrawCursor) {
1237 int pos[2];
1238 GetCursorPosition(window, pos);
1239 stub.spu->dispatch_table.ChromiumParametervCR(GL_CURSOR_POSITION_CR, GL_INT, 2, pos);
1240 }
1241 stub.spu->dispatch_table.SwapBuffers( window->spuWindow, flags );
1242 }
1243 else {
1244 crDebug("Calling SwapBuffers on a window we haven't seen before (no-op).");
1245 }
1246}
Note: See TracBrowser for help on using the repository browser.

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